2

On posix it is possible to use timespec to calculate accurate time length (like seconds and milliseconds). Unfortunately I need to migrate to windows with Visual Studio compiler. The VS time.h library doesn't declare timespec so I'm looking for other options. As far as could search is it possible to use clock and time_t although I could't check how precise is counting millisecons with clock counting.

What do you do/use for calculating time elapse in a operation (if possible using standards c++ library) ?

PLS
  • 231
  • 2
  • 6
  • 14

2 Answers2

2

The function GetTickCount is usually used for that.

Also a similiar thread: C++ timing, milliseconds since last whole second

Community
  • 1
  • 1
Grim
  • 937
  • 10
  • 24
  • Is there a way to use standards c++ library. It would be a bonus. I forgot to mention that as well – PLS May 31 '11 at 14:25
  • Check this thread, it contains an answer: http://stackoverflow.com/questions/117346/c-timing-milliseconds-since-last-whole-second – Grim May 31 '11 at 14:28
  • Thanks @Grim. It seen that this isn't portable – PLS May 31 '11 at 14:33
  • The boost solution on the referenced thread is probably your most portable solution. – Elemental May 31 '11 at 20:15
2

Depends on what sort of accuracy you want, my understanding is that clock and time_t are not accurate to the millisecond level. Similarly GetTickCount() is commonly used (MS docs say accurate to 10-15ms) but not sufficiently accurate for many purposes.

I use QueryPerformanceFrequency and QueryPerformanceCounter for accurate timing measurements for performance.

Elemental
  • 7,365
  • 2
  • 28
  • 33