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
  • 38,779
  • 79
  • 233
  • 389
  • 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

2 Answers2

1

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,285
  • 24
  • 28
  • Very helpful and straight to point, should be accepted answer! – ScienceDiscoverer Jun 30 '22 at 13:24
  • One note, this functions NEVER fails if you run it on Windows XP or later. So there is no point to check for return value! – ScienceDiscoverer Jun 30 '22 at 14:03
  • But its better to avoid doubles because of precision issues. Better to calc ns per tick like this `1 000 000 000/frequency`, then use that to find ns `ticks * ns/tick`, and then divide that by 1000 to get us, 1 000 000 for ms. This way you keep integer operations only, which improves precision and execution time. – ScienceDiscoverer Jun 30 '22 at 16:44
  • Forget what I said before, my central neural network seems not to be working today at all... Use this instead `ULL us = timing / (1000/ns_per_tick);` this avoids overflow after `timing` reaches 584.9424 years. – ScienceDiscoverer Jun 30 '22 at 17:03
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
  • 833
  • 5
  • 11