# iffy while loops

This topic is 4424 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Profile:Cpp,DevCpp,Windows... hey everyone, this question is about some curiosity I have with if statements and while loops that all those 1000 page textbooks don't cover. Here is the code(if you want to call it that) I am going to make reference to in a second:
while(blah)
{
if(GetTickCount() - looptime > 33)
{
looptime = GetTickCount();
//do timed operation
}

}


The if statement-in my program-is used to do a timed operation; about 30 frames per second. The While loop I am referring to is the Windows main event loop(the one that dispatches the messages and such). ok, now that I have established some background here comes the questions. 1. If I set the time in the if statement(currently at 33ms) to 0ms, shouldn't the if code block(//do timed operation) run almost exactly as fast as if it the IF statement did not exist. The reason I ask is that I commented out an if statement in my code, and it ran at about 1000 iterations per second. I assumed this was the "speed" of the while loop. Then I uncommented the If statement and set the time to 0 and it went at 55 iterations per second. I figured that GetTickCount() was the culprit so I replaced the whole if condition of the if statment with TRUE, and it ran at about 1000 iterations per second. Which leads me to my next question. 2.How efficient/inefficient is GetTickCount(). I used it inside my while statment and it did not seem to have the same dramatic effect as when I put it in the If condition. 3. How much effect do IF statments,and particulary SWITCH statments have on the speed of my program. I know that too many calls to functions will create "function overhead"(without inline) but what about these rudementary tools of programming? Thanks for all the help, also if anyone has a different-more efficient way-of creating a timed section of code(one that fires every X seconds), dont be bashful. (PS: If someone can point me to the DirectX7 help file I would be much obliged, I cant find it on the MS web site or in my sock drawer :))

##### Share on other sites
Function calls and if statements are extremely inexpensive operations. Their cost is only significant in extremely low level optimizations for code that is run very frequently (say, thousands of times per tick).

The reason why your loop appears to slow down with the if statement and the GetTickCount is becuase GetTickCount might only be returning a new value about 55 times per second. Try using if(GetTickCount() - looptime > -1) and your code will be back to running a thousand times a second.

##### Share on other sites
thanks, does anyone know of a better(faster) timer function other than GetTickCount() that I could use to speed up my program.

Much Thanks,

##### Share on other sites
There is nothing wrong with the GetTickCount() function. You are limited to a maximum of 1000 cycles per second because you are working with a millisecond timer.

With your current code, the value from GetTickCount() must be at least 1 greater than your stored looptime value before the if statement will be true.

if ( GetTickCount() - looptime >= TIMELIMIT )

where TIMELIMIT is the desired delay between cycles.

Now if you have a value of zero as the TIMELIMIT you should see the true 'speed' of the code.

##### Share on other sites
Windows is not a real-time operating system and doesn't have a really good timer. It does have some timing functions that are usually "good enough".

If you only need millisecond resolution, use:
//At the start of your programtimeBeginPeriod (1); //1ms resolution//In between, use timeGetTime exactly as you would GetTickCount//At the end of your programtimeEndPeriod (1); //undo 1ms resolution

Otherwise, check out QueryPerformanceCounter coupled with QueryPerformanceFrequency on MSDN.

##### Share on other sites
timeGetTime()

MSDN reference here

Of course that requires and additional library and will make your EXE slightly bigger.

Edit: There is also QueryPerformanceCounter(...) however I heard that it has problems with the 64bit processors (I could be wrong.)

Edit2: Wow! 2 people beat me too it by a minute! XD

##### Share on other sites
Awesome, thanks for everyone's help (pratings)