I know I can use the time(NULL) function to measure amounts of time as little as one second, but how can I measure less than one second?
I know I can use the time(NULL) function to measure amounts of time as little as one second, but how can I measure less than one second?
---Rainer
Digital pimp, hard at work
C++ can normally measure time down to the millisecond (1/1000 th of a second)... 1/2 second = 500ms.
There is a constant defined in time.h called CLOCKS_PER_SEC. This seems to always be 1000, but I get the feeling this is only a psuedo-standard (?)
The function clock() will return the number of counts (i.e. milliseconds).
On Windows machines, clock() will return the number of milliseconds, but it only gets updated about every 50 milliseconds... I've forgotten exactly.
See the Programming FAQ
Yeah, CLOCKS_PER_SEC is standard though it's value isn't. Values between 1 and 1000 seem usual though.
Also, clock() measures program time, not real time. If your program spends a lot of time sleeping or waiting for things to happen, then clock() will lag.
Basically, anything sub-second is specific to your OS/Compiler. So say what you have if you want a more specific answer.
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 want to be able to run my program in any win32 environment (since it's ANSI, it shouldn't matter, right?) and I have the worst compiler out there, Dev version 4. I've been meaning to get a better one...
---Rainer
Digital pimp, hard at work
If you need about a half-second you should be OK most of the time...
However, you simply cannot count on accurate timing (in user mode) with a multi-tasking operating system. In user mode, another program can interrupt your program at any time... maybe in the middle of your half-second count... When it returns to your program, it may be two seconds later. To get total control of the system, and lock-out other concurrent processes and interrupts, you have to get into the kernel mode. (Not easy stuff... and I don't know how to do it.)
There is a Windows function, I think it's GetTickCount(), that will return milliseconds accurately (at the time it's executed). But, of course that function will only execute during your program's time-slice.
Last edited by DougDbug; 11-18-2003 at 04:00 PM.