Slow response from function

This is a discussion on Slow response from function within the C++ Programming forums, part of the General Programming Boards category; In this program if i draw my scene using the version below which calls a function DrawBox, and Drawbox calls ...

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422

    Slow response from function

    In this program if i draw my scene using the version below which calls a function DrawBox, and Drawbox calls Drawpixel to actually create the scene it runs really slow, compared to if i remove the commetns and take out the call to DrawBox and just use Drawpixel directly, which then runs nice and fast. Is there a known reason for this?

    To clarify, the slow version is as seen below, if i remove the comments to recreates my original version and delete call to Drawbox it runs fast.

    Code:
    for(mainDwn = 0; mainDwn < dwnMax; mainDwn++)                                                       //draw the map
        {
                 for(mainAcr = 0; mainAcr < acrMax; mainAcr++)
                 {
                     if(mapnode[mainDwn][mainAcr].Walkstate)
                     {   cR = 128; cG = 128; cB = 255; }
                     else
                     {   cR = 0; cG = 0; cB = 255; }
    
                     if(mapnode[mainDwn][mainAcr].STnode)
                     {   cR = 255; cG = 128; cB = 0; }
                     if(mapnode[mainDwn][mainAcr].TGnode)
                     {   cR = 0; cG = 255; cB = 128; }
    
                             /* for(countY = 0; countY < 14; countY++)
                              {
                                         for(countX = 0; countX < 14; countX++)
                                         {
    
                                             DrawPixel(screen,boxAcr, boxDwn,cR,cG,cB);
                                                   boxAcr++;
                                         }
                                boxAcr -= 14;
                                boxDwn++;
                              } */
                              DrawBox(screen, mainDwn, mainAcr, cR,cG,cB);
    
                 // boxDwn -=14;
                  //boxAcr += 15;
                 }
    
       // boxAcr = 0;
       // boxDwn += 15;
      }
    here is the Drawbox function for reference also >

    Code:
    void DrawBox(SDL_Surface *screen, int cY, int cX, int cR, int cG, int cB)
    {
        int X_start_ref = cX * Boxsize;
        int boxY = cY * Boxsize;
        int boxX = cX * Boxsize;
        int maxboxY = boxY + Boxsize;
        int maxboxX = boxX+ Boxsize;
    
        while(boxY < maxboxY-1)
        {
            while(boxX < maxboxX-1)
            {
                DrawPixel(screen,boxX, boxY,cR,cG,cB);
                boxX++;
            }
        boxX = X_start_ref;
        boxY++;
        }
    
        SDL_Flip(screen);
    }

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    EDIT: Oh, nvm. Misread the code.

    RE-EDIT:

    What is the code supposed to do, exactly? Why are you nesting the topmost loops? And why on earth is your indenting so bad? Fix your indenting, the code is horrible to read.
    Last edited by IceDane; 09-21-2009 at 06:51 AM.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    it just draws boxes as a function of a pathfinding program, it works fine, just i wanted to tidy it up and i found that it runs slowly with the drawbox function in it, drawbox runs fine in other parts of the program, as far as the indenting goes i think i could do it like bloomin bill gates and i would still get somebody complaining, i know its not perfect but there is no hard and fast rule i have been pointed out to despite the links i have seen, just lots of talk about the different ways to do things so no way can anybody ever please everybody./

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    The commented code only makes 196 (14 * 14) iterations. I'm guessing that (cY * Boxsize) * (cX * Boxsize) is generally going to be a much larger number. They really involve completely different kinds of calculations, though.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Maybe the DrawPixel() makes it slow? Maybe use a Draw Rectangle already-made function instead to see if there is a differense?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One difference is that the function calls SDL_Flip, which isn't necessarily particularly fast either. Couldn't a profile tell something?

    On hardware that supports double-buffering, this function sets up a flip and returns. The hardware will wait for vertical retrace, and then swap video buffers before the next video surface blit or lock will return.
    This looks like something that could cause massive slowdown, if called very often.
    Last edited by anon; 09-21-2009 at 11:31 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by anon View Post
    One difference is that the function calls SDL_Flip, which isn't necessarily particularly fast either. Couldn't a profile tell something?



    This looks like something that could cause massive slowdown, if called very often.
    I posted that the first time I posted, but edited out because I didn't think it did. But sounds like that may be the reason.

    Also, since you're drawing a filled rectangle, I'm pretty sure using SDL_FillRect will be faster than whatever you're doing.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    yea, thanks, i think i was reinventing the wheel for no reason there, the reason i did not use the fill rectangle feature was because i thought it was only used to create new surfaces rather than draw shapes as such, but then i suppose that is just a relative thing.

    the bigger portion of 'mal-indented' code draws a 60*60grid of small squares to represent a map, the smaller funtion just colours individual squares on that map.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21