Getting time with Miliseconds

Started by
4 comments, last by FERNANDO-BRASIL 22 years, 3 months ago
Hi! I would like to know (in Visual C++ 6.0), how I could get the time in Milisenconds... I want a function ANSI compatible... Thank you very mutch guys!
Advertisement
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
Thank you a lot FrigidHelix, but I would like to know, a ANSI-COMPATIBLE function, that I can port to different C++ compilers...

Does anyone knows?
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
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
-Lutrosis#define WHOOPS 0class DogClass {public: CDog() { printf("Ruff!"); } Run() { printf("Run!"); } Crash() { printf("%d",100/WOOPS); }};DogClass CDog;CDog.Run();CDog.Crash();
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement