tags:

views:

418

answers:

3

How can I convert microseconds to milliseconds and make it possible to compare timestamps by substraction? Example:

int t1 = (int)timeGetTime();    
int t2 = (int)timeGetTime()+40;
// now t1 - t2 < 0 which means that t1 < t2.

This logic won't work if you divide time values by 1000, to convert microseconds to milliseconds.

A: 

Why not multiply the millisec values by 1000 so that you are comparing in microseconds? Alternatively, use floating point numbers instead.

spender
Floating point types for time domain data is generally counter-indicated: the clock either does or does not return a particular tick, so the data is integer in nature. Just use a big enough integer type.
dmckee
Float numbers will overflow eventually. With integers it doesnt matter, since usually they are used only to compare with other integer-time-values.
AareP
+1  A: 

The resolution of clock timers in Windows are restricted to about 10 milliseconds so you will never be able to get time values to the precision of microseconds.

If your time values are coming from somewhere else that is capable of that resolution then take the values as microseconds. Multiplying by 1000 or dividing an int by 1000 will not give you any better resolution it will just change the scale of your comparision.

Phill
Interesting chat about precision and accuracy from the infamous Raymond Chen: http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx
Phill
I know, my microsecond-values come from QueryPerformanceCounter. All I want is to have calculations in different units than microseconds. Getting tired writing all those zeroes, like: time/1000000...
AareP
Ok, first of all, even if you are getting a value in microseconds doesn't mean the value is accurate to that resolution as per the Raymond Chen article. Secondly, I don't really get what you're asking here, is it just that you want to be able to convert to seconds and don't like typing zeroes? Use a constant. Third, if you really want to do this then just use a double: double milliseconds = ((double)microSeconds) / 1000d (or c++ equivalent or this). I still think you may be misunderstanding the underlying issue here though.
Phill
A: 

You'll need to use a high resolution timer to get microsecond granular time.

You can use the windows API to check how granular you can make it.

QueryPerformanceFrequency(&ticksPerSecond); QueryPerformanceCounter(&tick);

These two functions will help you along that way. Take a look at the MSDN articles for more. :)

Xorlev