0

I'm trying to figure out how to time the execution of part of my program, but when I use the following code, all I ever get back is 0. I know that can't be right. The code I'm timing recursively implements mergesort of a large array of ints. How do I get the time it takes to execute the program in milliseconds?

//opening input file and storing contents into array
index = inputFileFunction(inputArray);

clock_t time = clock();//start the clock

//this is what needs to be timed
newRecursive.mergeSort(inputArray, 0, index - 1);

//getting the difference        
time = clock() - time;

double ms = double(time) / CLOCKS_PER_SEC * 1000;

std::cout << "\nTime took to execute: " << std::setprecision(9) << ms << std::endl;
anna
  • 37
  • 4
  • 2
    `clock()` only has second precision (and on a 32bit system it can wrap every 36 minutes), so if `mergeSort()` takes less than 1 second to run, that would explain why you get a result of 0. For what you are attempting, you need to use a higher-precision timer, such as from `clock_gettime()`, or even an OS-specific API. – Remy Lebeau Aug 08 '16 at 21:41

2 Answers2

6

You can use the chrono library in C++11. Here's how you can modify your code:

#include <chrono>
//...
auto start = std::chrono::steady_clock::now();
// do whatever you're timing
auto end = std::chrono::steady_clock::now();
auto durationMS = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "\n Time took " << durationMS.count() << " ms" << std::endl;
grigor
  • 1,584
  • 1
  • 13
  • 25
  • Sorry in the last line of cout you need durationMS.count() instead of durationMS – grigor Aug 08 '16 at 21:48
  • thank your for your help. when i implemented your code though it's still giving me an output of 0 :( – anna Aug 08 '16 at 21:52
  • Do you think it's running in less than 1 microsecond? Maybe try `nanoseconds` instead of microseconds in `duration_cast`? – grigor Aug 08 '16 at 21:55
  • @grigor I think you're right. I increased the size of the data that the recursive mergesort has to go through so it takes longer, and something other than 0 finally showed up. Is there anything smaller than nanoseconds? – anna Aug 08 '16 at 22:01
  • @drescherjm Initially I was sorting through an array of 1000 ints, which yielded a result of 0. I had to bump up the array size to 45,000 to get something other than 0. – anna Aug 08 '16 at 22:04
  • Ok glad it worked. I don't think there is anything smaller than nanoseconds. A cpu cycle takes roughly a nanosecond, so you can't really time anything less than that. – grigor Aug 08 '16 at 22:07
  • 1
    Rarely will you have a clock that can resolve to nanoseconds. C++ allows you to specify nanoseconds because why not? Costs almost nothing and you don't have to rewrite that part of the standard later. – user4581301 Aug 08 '16 at 22:18
  • Re: Smaller than nanoseconds: You can create your own picoseconds if you want: `using picoseconds = std::chrono::duration;`. That won't make your clock more accurate. But it can come in handy when averaging: http://stackoverflow.com/a/11485388/576911 – Howard Hinnant Aug 09 '16 at 19:54
0

If you're developing on OSX, this blog post from Apple may be useful. It contains code snippets that should give you the timing resolution you need.