For loop takes time

This is a discussion on For loop takes time within the C++ Programming forums, part of the General Programming Boards category; I have a very weird (at least how I see it) problem. In a game I am making I perform ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205

    For loop takes time

    I have a very weird (at least how I see it) problem. In a game I am making I perform some memory copying using a for loop and when I loop through it 512*512 times (262144 times) it is very very very slow, but when I loop through it 600*650 times (390000 times) the loop goes around 4 times as fast. Anybody have any idea what is going on here? I am performing the same operations in both cases and the timing is done the same way in both cases.
    Last edited by Shakti; 10-13-2004 at 01:58 AM.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    I'd love to see some code ^^

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    Here is the loop itself.
    Code:
    for (DWORD screencounter=0;screencounter<screensize;screencounter++)
    {
    	bgBuffer[screenoffset]=imageBuf[spritepixeloffset];
    
    	screenoffset+=screenX;
    	spritepixeloffset+=sizeX;
    
    	ycounter++;
    	spriteycounter++;
    
    	if (spriteycounter >= sizeY) 
    	{
    		spritepixeloffset=spritexcounter;
    		spriteycounter=0;
    	}
    
    	if (ycounter>=screenY)
    	{
    		ycounter=0;
    		xcounter++;
    		spritexcounter++;
    		spriteycounter=spritepixely;
    		if (spritexcounter>=sizeX) spritexcounter-=sizeX;
    		
                    spritepixeloffset=spriteybottom+spritexcounter;
                
    		screenoffset=xcounter;
    	}
    }
    imageBuf is a buffer of DWORDs with 256 elements. bgBuffer is a buffer with the size mentioned in my first post.
    Last edited by Shakti; 10-13-2004 at 09:10 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Check the alignment of the destination buffer in both cases (just print its address in hex format).

    If the address doesn't end in 0, 4, 8 or C, then it's unaligned memory, which can be a lot more expensive to access than aligned memory.

    0x2468 is an aligned address
    0x1357 isn't
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    The memory is aligned.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    What happens if you do the 600*650 test before the 512*512 test?
    Do you re-use the same (large) buffer for both tests?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    The size of the buffer is at the moment decided compiletime but allocated dynamicly, this is then updated every frame when a camera moves.
    Last edited by Shakti; 10-13-2004 at 11:47 PM.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I take it that it is repeatable? (And hence, not the system allocating cpu time for something else instead of your program on that given test.)
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    It happends every time I run it.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I figured... Just something to check

    Not sure it'll help too much, but can we see some more relevant code?
    And, what system, compiler, etc is this occuring on?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    ok here is the whole function:
    Code:
    DWORD screenY = 600;
    DWORD screenX = 650;
    
    DWORD addr1=0,addr2=0;
    
    if(bgBuffer == NULL)
    {
    	try
    	{
    		bgBuffer = new DWORD[screenY*screenX];
    	}
    	catch(std::bad_alloc error)
    	{
    		MessageBox(0, error.what(), 0, 0);
    		PostQuitMessage(0);
    	}
    }
    
    int camX = (int)cam->GetBgX();
    int camY = (int)cam->GetBgY();
    
    DWORD screenoffset=0;
    int imgwidth=sizeX;
    int spritepixelx=((camX) % sizeX);
    int spritepixely=((camY) % sizeY);
    if(spritepixelx < 0 || spritepixely < 0)
    	return;
    
    WORD spritepixeloffset=(spritepixely*sizeX)+spritepixelx; //This should be spritepixelx
    WORD ycounter=0;
    WORD xcounter=0;
    
    DWORD screensize=screenY*screenX;    
        
    WORD spritexcounter=spritepixelx;
    WORD spriteycounter=spritepixely;
    spriteycounter = spriteycounter;
    
    WORD spriteybottom = spritepixely*sizeX;
    
    for(DWORD screencounter=0;screencounter<screensize;screencounter++)
    {
    	bgBuffer[screenoffset]=imageBuf[spritepixeloffset];
    
    	screenoffset+=screenX;
    	spritepixeloffset+=sizeX;
    
    	ycounter++;
    	spriteycounter++;
    
    	if (spriteycounter >= sizeY) 
    	{
    		spritepixeloffset=spritexcounter;
    		spriteycounter=0;
    	}
    
    	if (ycounter>=screenY)
    	{
    		ycounter=0;
    		xcounter++;
    		spritexcounter++;
    		spriteycounter=spritepixely;
    		if (spritexcounter>=sizeX) spritexcounter-=sizeX;
    		spritepixeloffset=spriteybottom+spritexcounter;
                
    		screenoffset=xcounter;
    	}
    }
    
    if(!glIsEnabled(GL_TEXTURE_2D))
    	glEnable(GL_TEXTURE_2D);
    if(bgTexture)
    	glDeleteTextures(1,bgTexture);
    
    glGenTextures(1, bgTexture);
    glBindTexture(GL_TEXTURE_2D, bgTexture[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    
    
    glTexImage2D(GL_TEXTURE_2D, 0, 4, screenX, screenY, 0, 
    			GL_RGBA, GL_UNSIGNED_BYTE, (DWORD*)bgBuffer);
    
    glDisable(GL_TEXTURE_2D);
    This happends on a winXP home system, AMD Athlon 2200+, and 512 ram with VS.NET 2002.
    Last edited by Shakti; 10-14-2004 at 08:25 AM.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    typo...

    Quote Originally Posted by Shakti
    Code:
    for(DWORDscreencounter=0;screencounter<screensize;screencounter++)
    I know you had it correct in the top post, just noting the typo here...

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    Oh yah, it became like that when I pasted it. Should be fixed now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 06:17 AM
  4. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 06:11 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

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