ElapsedMilliseconds for C/C++

For everything that's not in any way related to PureBasic. General chat etc...
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

ElapsedMilliseconds for C/C++

Post by Mistrel »

From what I understand, time() is implementation-specific. So I wrote a replacement using Win32. if anyone has a way of doing this in a standardized way that is more portable, I'm all ears.

I programmed this for C++. I'm assuming that it should work fine in C as well.

Code: Select all

INT64 elapsedMilliseconds() {
   SYSTEMTIME sysTime;
   FILETIME elapsed;
   INT64 posix;
   
   GetSystemTime(&sysTime);
   SystemTimeToFileTime(&sysTime, &elapsed);
   
   // FILETIME to POSIX
   posix = *(INT64*)&elapsed;
   
   posix = (posix - 116444736000000000) / 10000;
   
   return posix;
}
Last edited by Mistrel on Thu Dec 09, 2010 7:41 am, edited 2 times in total.
freak
PureBasic Team
PureBasic Team
Posts: 5941
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: ElapsedMilliseconds for C/C++

Post by freak »

I moved this to OffTopic. The Tips section is for PureBasic codes.
quidquid Latine dictum sit altum videtur
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: ElapsedMilliseconds for C/C++

Post by Joakim Christiansen »

Helping Kwaï chang caïne or something? :o
I like logic, hence I dislike humans but love computers.
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: ElapsedMilliseconds for C/C++

Post by idle »

was there some problem with using GetTickCount_() ?
Windows 11, Manjaro, Raspberry Pi OS
Image
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: ElapsedMilliseconds for C/C++

Post by Nituvious »

idle wrote:was there some problem with using GetTickCount_() ?
He may have wanted a cross platform function.
▓▓▓▓▓▒▒▒▒▒░░░░░
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: ElapsedMilliseconds for C/C++

Post by DarkDragon »

Nituvious wrote:
idle wrote:was there some problem with using GetTickCount_() ?
He may have wanted a cross platform function.
Euh.. but GetSystemTime and SystemTimeToFileTime are more WinAPI functions than one GetTickCount or timeGetTime.
bye,
Daniel
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: ElapsedMilliseconds for C/C++

Post by idle »

I was just wondering if there was a difference with GetTickCount_() on 64 bit systems
Windows 11, Manjaro, Raspberry Pi OS
Image
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: ElapsedMilliseconds for C/C++

Post by Mistrel »

GetTickCount_() has a 49 day resolution.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: ElapsedMilliseconds for C/C++

Post by Rescator »

idle wrote:I was just wondering if there was a difference with GetTickCount_() on 64 bit systems
Nope, which is why you should use GetTickCount64 it's only Windows 6.x or later but it gives you like hundreds of years before it wraps?
Post Reply