1

I am timing how long it takes to do three different types of searches, sequential, recursive binary, and iterative binary. I have those in place, and it does iterate through and finish the search. My problem is that when I time them all, I get 0 for all of them every time, even if I make an array of 100,000, and I have it search for something not in the array. If I set a break point in the search it obviously makes the time longer, and it gives me a reasonable time that I can work with. But otherwise it is always 0. Here is my code, it is similar for all three search timers.

 clock_t recStart = clock();
 mySearch.recursiveSearch(SEARCH_INT);
 clock_t recEnd = clock();
 clock_t recDiff = recEnd - recStart;
 double recClockTime = (double)recDiff/(double)CLOCKS_PER_SEC;
 cout << recClockTime << endl;

 cout << CLOCKS_PER_SEC << endl;

 cout << recClockTime << endl;

For the last two I get 1000 and 0.

Am I doing something wrong here? Or is it in my search Object?

SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • 2
    Ever since I found boost's `auto_cpu_timer`, I've never looked back. It takes care of all those things for you. Instantiate the timer, let it go out of scope, voila. – us2012 Feb 17 '13 at 02:34
  • 1
    Run your search in a loop (e.g., a million times). Then, for the time you recorded, divide by the number of iterations to get the time for a single call to search. – jxh Feb 17 '13 at 02:45
  • 1
    You made a point by adding *roughly* but I suppose *roughly* should be something like 0.2s, which `clock()` seems to work fine here. – phoeagon Feb 17 '13 at 02:47
  • 1
    Yeah, that's what I would guess it would take, just 0.2, but I get zero, even if I do set it to search through 1,000,000 iterations. Does this code look okay then? Is it somewhere else where my problem is? – SirRupertIII Feb 17 '13 at 02:49
  • @KKendall: What does your million iteration code look like? What do you see when you output `recDiff` and `CLOCKS_PER_SEC` individually? – jxh Feb 17 '13 at 03:14
  • 1
    @KKendall: Compare your code with: http://ideone.com/CXajTQ – jxh Feb 17 '13 at 03:23
  • @user315052 made an edit – SirRupertIII Feb 17 '13 at 03:31
  • http://stackoverflow.com/questions/11604336/microtime-equivalent-for-c-and-c – Grady Player Feb 17 '13 at 04:00
  • @KKendall: You can either use a higher resolution clock, or you can change your code so that you give your clock something it can measure. There are no other alternatives. – jxh Feb 17 '13 at 04:02

2 Answers2

3

clock() is not an accurate timer, and it just don't work well for timing short intervals.

C says clock returns the implementation’s best approximation to the processor time used by the program since the beginning of an implementation-defined era related only to the program invocation.

If between two successive clock calls you program takes less time than one unity of the clock function, you could get 0. POSIX clock defines the unity with CLOCKS_PER_SEC as 1000000 (unity is then 1 microsecond).

(http://pubs.opengroup.org/onlinepubs/009604499/functions/clock.html)

To measure clock cycles in x86/x64 you can use assembly to retreive the clock count of the CPU Time Stamp Counter register rdtsc. (which can be achieved by inline assembling?) Note that it returns the time stamp, not the number of seconds elapsed. So you need to retrieve the cpu frequency as well.

However, the best way to get accurate time in seconds depends on your platform.


To sum up, it's virtually impossible to achieve calculating and printing clock_t time in seconds accurately. You might want to see this on Stackoverflow to find a better approach (if accuracy is top priority).

Community
  • 1
  • 1
phoeagon
  • 2,080
  • 17
  • 20
2

clock() just doesn't have enough resolution - here is one good discussion/blog on that topic http://www.guyrutenberg.com/2007/09/10/resolution-problems-in-clock/

I think two options either use clock_gettime or even better have you considered using OProfile or CodeAnalyst?

I personally prefer to use tools - OProfile is good. I have not used CodeAnalyst before - and then there is Valgrind and gprof.

If you insist on using clock_gettime - please check this out http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/

abRao
  • 2,787
  • 1
  • 25
  • 37