# calculate execution time in nanoseconds

### Question

• In Visual C++ how to calculate execution time in nanoseconds?i am getting result via clock() function is 0 so how to get in nanoseconds....

thanks

Thursday, February 07, 2013 4:48 AM

• Hi,

you are correct - the clock() call is just giving back miliseconds (1/CLOCKS_PER_SEC which is 1/1000 with Microsoft C).

But more accurate values are quite hard to get. The core problem is, that we get into a range where you measue single CPU actions.

Maybe http://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-nano-seconds is helpfull for you. There are some suggestions on this topic including examples how you can get real ticks (which do not give you an exact time). And at the moment I am not fully aware: If during a test cycle the CPU changes the speed, you might simply get wrong results.

With kind regards,

Thursday, February 07, 2013 9:50 AM
• The easiest timer to use when trying to measure execution times is perhaps QueryPerformanceCounter which you can find out how many "counts/sec" that refers to by calling QueryPerforanceFrequency.

Both functions take a pointer to a 64 bit integer into which they store the result.   The math to convert to nanoseconds is left as an excercise for the student:

```LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
double nanoseconds_per_count = 1.0e9 / static_cast<double>(freq);

LARGE_INTEGER time1, time2;
QueryPerformanceCounter(&time1);
function_to_be_timed();
QueryPerformanceCounter(&time2);

int nanoseconds = (time2-time1)*nanoseconds_per_count;
```

Friday, February 08, 2013 11:26 AM

### All replies

• Hi,

you are correct - the clock() call is just giving back miliseconds (1/CLOCKS_PER_SEC which is 1/1000 with Microsoft C).

But more accurate values are quite hard to get. The core problem is, that we get into a range where you measue single CPU actions.

Maybe http://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-nano-seconds is helpfull for you. There are some suggestions on this topic including examples how you can get real ticks (which do not give you an exact time). And at the moment I am not fully aware: If during a test cycle the CPU changes the speed, you might simply get wrong results.

With kind regards,

Thursday, February 07, 2013 9:50 AM
• Hello Pince123,

I have moved this thread to Visual C++ for better reponse.

Best regards,

Amanda Zhu [MSFT]
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Friday, February 08, 2013 8:03 AM
• I don't think that you will be able to get a result in nano seconds resolution with a defined deviation.

Even on realtime operating systems or embedded systems a nano seconds resolution is not a trivial thing. In most cases you use on embedded systems a hardware time exclusively only for the calculation of this special nano seconds execution time calculation. windows is not a real time operating system at al. With multimedia timers you may get time resolutions in microseconds with a tolerance that may be accepted, but this is far away from nano seconds with and acceptable tolerance.

You should be aware on getting a nano seconds resolution on a PC. It may be possible that you will get some values that have a walue in a nano seconds range, but the next question is the precision of this values.

Best regards

Bordon

Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.

Friday, February 08, 2013 9:31 AM
• The easiest timer to use when trying to measure execution times is perhaps QueryPerformanceCounter which you can find out how many "counts/sec" that refers to by calling QueryPerforanceFrequency.

Both functions take a pointer to a 64 bit integer into which they store the result.   The math to convert to nanoseconds is left as an excercise for the student:

```LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
double nanoseconds_per_count = 1.0e9 / static_cast<double>(freq);

LARGE_INTEGER time1, time2;
QueryPerformanceCounter(&time1);
function_to_be_timed();
QueryPerformanceCounter(&time2);

int nanoseconds = (time2-time1)*nanoseconds_per_count;
```

Friday, February 08, 2013 11:26 AM
• ok .I am having the problem with value return by clock() in visual c++ ,it only gives the value in multiple of 16 but not between them why it is happening ?

thanks

Saturday, February 09, 2013 2:15 PM
• Based on the fact that CLOCKS_PER_SEC is 1000, it can be inferred that it is implemented via GetTickCount.  Further, looking at GetTickCount it likely updates only every 16 ms or so, so it makes sense that you are seeing what you are seeing.  Try QueryPerformanceCounter as Ron suggested for hi precision timing.  The actual frequency will vary by processor, although I have seen 1 ns frequency on some CPUs.
Saturday, February 09, 2013 2:53 PM
• ok i am seeing on Desktop CPU (dual core )multiple of 16 while,when  i see on laptop CPU(i5) which is newer one ,it gives difference of 1ms then i multiply by 1000000 to get nanosecond.For same dataset difference,which CPU should i prefer for correct time precision if i use clock() technique (by taking difference between msec and then multiply by 1000000)

thanks

Saturday, February 09, 2013 4:50 PM
• Obviously if you are looping on clock() and finding out that the increment on one computer is 1 ms, then that computer has a finer underlying tick count, and that computer would be preferred if you are using clock() as a means of determining elapsed time. However, your accuracy is going to only be plus or minus 1 msec, even though you are converting to nanoseconds.

As two other posters have already pointed out, QueryPerformanceCounter() will give you a much more accurate value of elapsed time, and I encourage you to take a look at that approach in preference to clock().

Saturday, February 09, 2013 7:14 PM
• ok thanks RON but what is the header file which i should use for function

QueryPerformanceFrequency(&freq);

QueryPerformanceCounter(&start);

I am not able to run the code which you have provided

i am using visual c++and header file <windows.h>

thanks

• Edited by Sunday, February 10, 2013 5:01 AM
Sunday, February 10, 2013 4:08 AM
• ok thanks RON but what is the header file which i should use for function QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);

The documentation tells you which header you need:
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms644905(v=vs.85).aspx

Those should be available by including windows.h - though it's
possible that your project has a definition to minimise the usage of
it - see the discussion on WIN32_LEAN_AND_MEAN here:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx

Dave

Sunday, February 10, 2013 9:52 AM