Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ get milliseconds since some date

Tags:

c++

time

I need some way in c++ to keep track of the number of milliseconds since program execution. And I need the precision to be in milliseconds. (In my googling, I've found lots of folks that said to include time.h and then multiply the output of time() by 1000 ... this won't work.)

like image 604
JnBrymn Avatar asked Jul 19 '10 18:07

JnBrymn


People also ask

Which function returns the time difference in milliseconds?

You can use DiffSeconds() built-in function and multiply the result by 1000. The output will be milliseconds.

How do I get milliseconds in Linux?

date +"%T. %6N" returns the current time with nanoseconds rounded to the first 6 digits, which is microseconds. date +"%T. %3N" returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.

Can Time_t store milliseconds?

time_t almost always represents seconds. Check out clock_t instead. It often has nanosecond precision, you can get the exact amount with the CLOCKS_PER_SEC macro, and you get a clock_t by calling clock() (instead of time() ).


2 Answers

clock has been suggested a number of times. This has two problems. First of all, it often doesn't have a resolution even close to a millisecond (10-20 ms is probably more common). Second, some implementations of it (e.g., Unix and similar) return CPU time, while others (E.g., Windows) return wall time.

You haven't really said whether you want wall time or CPU time, which makes it hard to give a really good answer. On Windows, you could use GetProcessTimes. That will give you the kernel and user CPU times directly. It will also tell you when the process was created, so if you want milliseconds of wall time since process creation, you can subtract the process creation time from the current time (GetSystemTime). QueryPerformanceCounter has also been mentioned. This has a few oddities of its own -- for example, in some implementations it retrieves time from the CPUs cycle counter, so its frequency varies when/if the CPU speed changes. Other implementations read from the motherboard's 1.024 MHz timer, which does not vary with the CPU speed (and the conditions under which each are used aren't entirely obvious).

On Unix, you can use GetTimeOfDay to just get the wall time with (at least the possibility of) relatively high precision. If you want time for a process, you can use times or getrusage (the latter is newer and gives more complete information that may also be more precise).

Bottom line: as I said in my comment, there's no way to get what you want portably. Since you haven't said whether you want CPU time or wall time, even for a specific system, there's not one right answer. The one you've "accepted" (clock()) has the virtue of being available on essentially any system, but what it returns also varies just about the most widely.

like image 100
Jerry Coffin Avatar answered Oct 17 '22 20:10

Jerry Coffin


See std::clock()    

like image 34
cpx Avatar answered Oct 17 '22 21:10

cpx