1

Possible Duplicate:
Windows C++ nanosecond timing?

I want to measure a function execution time. But I use old C++ (I mean I can't use chrono) and I am on windows. I cold not find any code snippet that does what I need. Please help.

Community
  • 1
  • 1
Narek
  • 36,981
  • 76
  • 218
  • 375
  • 3
    Use [`boost::chrono`](http://www.boost.org/doc/libs/1_51_0/doc/html/chrono.html) for compatibility instead. – Steve-o Oct 18 '12 at 14:09
  • What is the time stamp function for boost chrono?? – Narek Oct 18 '12 at 14:14
  • You don't need such precision on Windows. It is not real-time system and there is no use for nanoseconds (you can not trust its value, anyway). The most you can get on Windows is milliseconds using Win32 API SYSTEMTIME structure . You can get the answer here: http://stackoverflow.com/questions/10654258/get-millisecond-part-of-time/10655106#10655106 – SChepurin Oct 18 '12 at 14:29
  • My function execution time in microseconds is 0, i need nanoseconds. – Narek Oct 18 '12 at 14:33
  • What function? Can you show it? – SChepurin Oct 18 '12 at 14:36
  • E.g. a function that adds to numbers. – Narek Oct 18 '12 at 14:37
  • Assembly? You can not trust the time you can get. As answered to you here http://stackoverflow.com/questions/12956956/get-time-stamp-via-boost-chrono-in-resolution-of-nanoseconds on 4 Mhz performance counter, a tick lasts 250 ns. Just for example. – SChepurin Oct 18 '12 at 14:45

3 Answers3

1

On Windows you have the option of using the high performance counter to get high resolution timing. This should be supported on even very old Windows installations and old compilers.

Telgin
  • 1,549
  • 9
  • 10
0

take a look at QueryPerformanceCounter Elapsed time can be obtained in ticks and you know the number of ticks per second. It may not be reliable over large intervals because the CPU speed may change.

Marius
  • 823
  • 5
  • 10
0

You can use the QueryPerformanceCounter

LARGE_INTEGER liFrequency = {0};

// Get the Frequency
if(QueryPerformanceFrequency(&liFrequency))
{

    // Start Timing
    LARGE_INTEGER liStart = {0};
    if(QueryPerformanceCounter(&liStart))           
    {
        // Do Stuff

        // Get Time spent...
        LARGE_INTEGER liStop = {0};
        if(QueryPerformanceCounter(&liStop))    
        {               
            LONGLONG llMilliseconds = (LONGLONG)((liStop.QuadPart - liStart.QuadPart) * 1000.0 / liFrequency.QuadPart);
            printf("time ellapsed ms:%lld\n", llMilliseconds);
        }
    }       
}
João Augusto
  • 2,265
  • 25
  • 28