tags:

views:

321

answers:

6

HOw do I convert from TickCounts to Milliseconds?

this is what I used:

long int before = GetTickCount();
long int after = GetTickCount();

I want the difference of it in seconds.

Thanks!

+8  A: 
int seconds = (after - before) /1000;
Priyank Bolia
+1  A: 

I'm not sure what OS/platform you're using, but there should be a call that returns the tick time in milliseconds.

time = after - before * <tick time in milliseconds>;


Edit:

I see that this is a Windows function that returns milliseconds already. The other answers are better.

Richard Pennington
Converting to seconds is an exercise for the reader. ;-)
Richard Pennington
+1  A: 

GetTickCount() returns the time in milliseconds. so (after - before)/<milli equivalent> should give you time in seconds

aJ
"milli equivalent", of course, is 1000, since a millisecond is 1/1000 of a second. :-)
Ken White
+4  A: 

for more precision, there is also QueryPerformanceCounter()

jlru
A: 

Did you try use boost? There is a type time_duration for this, and functions like seconds(), nanoseconds(), miliseconds()

More information

coelhudo
The question was about converting TickCounts to milliseconds. Adding in a whole framework to explain how to do division seems like the wrong answer to me.
Ken White
+1  A: 
int seconds = (after - before + 500) / 1000;

or:

double seconds = (after - before) / 1000.0;
Hans Passant
Why "+ 500"? The OP didn't specify whole seconds, so why rounding up?
Ken White
Beware the integer division, it doesn't generate a floating point value. Adding 500 doesn't round up, it rounds.
Hans Passant