{steinsoft.net}
 
 
 
 
 
Home/News
Code Snippets
Board
Projects
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Leipzig
Home :: Programming :: Code Snippets :: Cpp :: Platform Indepentent Timer

[ Platform Indepentent Timer ]

Do you want to program an application that uses a timer that uses a relatively high resolution timer (milliseconds) and that can be at the same time compiled on other OS's? Then is this the right code snippet for you ;-). I will show you a function that returns the elapsed processor time in ms. Here it is:

#include <time.h>
 
float getElapsedTimeInMs()
{
   //Return clock() as ms
   //1 Second = 1000 milliseconds
   return clock()/(CLOCKS_PER_SEC/1000);
}

The standard function clock() returns the clock ticks of elapsed processor time. The constant CLOCKS_PER_SECS holds the number of clocks the processor does in one second. Under Windows, this constant equals 1000 so you could simply return clock() without the other mathematics.

But this value may vary on other platforms. So what we do, is to divide CLOCKS_PER_SEC first by 1000. As clock() divided by CLOCKS_PER_SEC would return seconds, CLOCKS_PER_SEC/1000 will return ms (1000 ms = 1 second).

Mail me if you have problems, suggestions or whatever. Happy Coding!

 Last edited 2002-12-07 18:39:30 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0