0

The thing is about getting time in C++ in form of milliseconds or nanoseconds. However I only know the way to get time in seconds like this:

#include <ctime>
...
time = ctime(0) ;
...

I want to know how to do similar operation to get time in milliseconds or nanoseconds. What headers I should include, what functions to call and what are their arguments ?

Useless
  • 64,155
  • 6
  • 88
  • 132
user1978386
  • 237
  • 8
  • 19

1 Answers1

1

If you are ok with a platform specific implementation (windows) look at the QueryPerformanceCounter API.

And this answer has a quick example of usage.

Community
  • 1
  • 1
dkackman
  • 15,179
  • 13
  • 69
  • 123
  • 2
    "look at QueryPerformanceCounter" - there lies madness... even when it chooses to use the promising RTC register, many issues including generally being unable to relate clock counter samples taken on one CPU core to those taken on another (and variable clock speeds in power saving modes, issues around hiberation etc.). But, at least it's sometimes usable as is for desktops timing a single thread bound to a specific core.... – Tony Delroy May 15 '13 at 14:24
  • this answer looks to be most usefull for me and makes pretty accurate timing – user1978386 May 15 '13 at 14:34
  • Madness! Madness I say! bwhahaha... Any high resolution timer is going to be impacted by the quirks of the system on which it runs. Same applies to boost (see caveats in their doco and prolly std lib though i've never used that). Might not use it to calibrate a pace maker but QPC has worked well for me in "everyday" usage. – dkackman May 15 '13 at 14:53