Archived

This topic is now archived and is closed to further replies.

Getting time with Miliseconds

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

One function you can use, is timeGetTime():

DWORD timeGetTime(VOID);

- Returns the system time in milliseconds

This function is part of the windows multimedia component, so, in order to use it, you will need to insert the following in your source files:
    
#include <mmsystem.h>

#pragma comment(lib,"winmm.lib")


Note, the pragma directive is a shortcut way of adding the library to your project. You can remove that line, and just go to project options, and add the library to the compiler and linker section.

I hope I didnt mistype anything, im just going off of memory here.

Edited by - frigidHelix on January 1, 2002 10:26:09 PM

Share this post


Link to post
Share on other sites
Well! Let's reformulate the question...

All that I wanna do, is this:

while (*****) {

// and here, for example, in each 40 miliseconds call another function

}

I don't want to use timers (like SetTimer), then, I have thought doing that like this way:

end = timeinmiliseconds();
while (***) {
start = timeinmiliseconds();
if (start > end + 40) {
// call some function
end = start;
}
}

This code, will wait for 40 miliseconds and call 'some function' every time... But the problem is, that I don't know a function (ANSI-COMPATIBLE) to get the time in miliseconds... ( I know just _ftime() - uses "sys/timeb.h" - but, it isn't ANSI-COMPATIBLE)...


thank you again guys!

Edited by - FERNANDO-BRASIL on January 1, 2002 10:54:15 PM

Share this post


Link to post
Share on other sites
Well this still might not be exactly what you''re looking for, since it won''t work on all hardware configurations, but have you considered QueryPerformanceCounter() and QueryPerformanceFrequency()? They''re part of the Win32 api, so you don''t have to worry too much about compiler portability, or even portability between languages.

They''re very simple to use, and provide great accuracy compared to the system timer. You might have to be careful if the compiler you''re using doesn''t support 64-bit integers, but if you''re using msvc you''re ok. Just call QueryPerformanceFrequency() to a) make sure there''s one available, and to b) get ahold of the frequency it''s operating at. Then you calculate the passage of time in milliseconds based on the difference of two calls to QueryPerformanceCounter() divided by the value obtained from QueryPerformanceFrequency multiplied by 1000.

Any use to you?

-Lutrosis

Share this post


Link to post
Share on other sites
Timing is very hardware specific, and unfortunetly no ANSI C function exist that garuantees timing to the millisecond.
Oh the results might be _in_ milliseconds, but it won''t be updated _every millisecond.

clock() or time() will kinda-sorta work.
#include <time.h>

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites