Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Getting precision in tiiming code segments

Posted on 2003-03-25
Medium Priority
Last Modified: 2010-04-01
i am currently using the typical time.h's clock() to time sorting algorithms. the results come in milliseconds but i need more precision possibly microseconds because when i sort small arrays i get 0 ms for a result when using (tv2 - tv1)/(CLOCKS_PER_SEC / (double) 1000.0)... timing experimentation is expected to reach up to an hour and i've heard/read somewhere that the reported clock ticks eventually reset when a max is reached so maybe clock() isn't my best bet... coding is done in microsoft visual c++ but will be compiled and timed in a unix environment.
Question by:dowhiles_donothing
  • 2
  • 2
LVL 12

Accepted Solution

Salte earned 225 total points
ID: 8204183
For one thing, if you want to do any timing you should sort many times. Sort the same array a zillion times or so and then divide the result by a zillion to get the timing for one run. This improves the accuracy.

So for one thing you should NEVER just sort once and then take the time. There are too many random things that will interfere and the result won't be reliable. In particular it will be less reliable the more accurate timer you use. If you use a very rough timer which measure days it will say 'it took one day' and that is very acurate, no matter how many times you sort it, I am sure that any timer will return a value less than 24 hours for the sort.

While the value from a microsecond timer will one time be X and the next time be Y and the difference in microseconds between X and Y may be rather large.

So, if you want to test how long time it takes to sort a specific array do it this way:

timer T1 = get_time();
// now do everything you do in the sort process except
// the sorting
// do it like N times where N is some big number (1000000 for example).

timer T2 = get_time();

T1 - T2 is the time it takes to do everything except the sorting N times.

// now do it all over again but this time also do the sorting.

timer T3 = get_time();

T3 - T2 is the time it takes to do everything + the sort.
(T3 - T2) - (T2 - T1) is the time it takes to do everything + sort - the time it takes to do everything except the sort.

I.e. it is the time it takes to do the sort N times.

(T3 - 2*T2 + T1)/N is the time it takes to sort.

The code between T1 and T2 and between T2 and T3 is almost identical. The only difference is that the code between T2 and T3 has a call to the sort function while the code between T1 and T2 lacks that call. Otherwise they are the same.

Now, if you want a very accurate timer, a pentium has that, it is the system timer and the fastest way to read it is to execute the assembly instruction rdtsc (read time stamp counter) This returns a 64 bit integer counting number of 0.1 microseconds. Windows sets this counter so that the absolute value count the number of 0.1 microseconds since Midnight January 1st 1601. However, you are not interested in the absolute value but only in the relative value and as such you don't have to worry about the meaning of the number, you just want the differences (the relative values).

__int64 get_time()
   __int64 x;

   __asm {
      mov dword ptr x,eax    // move low 32 bits
      mov dword ptr x + 4,edx  // move high 32 bits.
   return x;


Expert Comment

ID: 8204402
On UNIX, you should use the gettimeofday function to get the high resolution time.  Note that although gettimeofday returns a timeval struct, in which the tv_usec value is the number of microseconds, I don't believe that all UNIX platforms guarantee that resolution.  For example, on some systems, the tv_usec may be rounded to the nearest ten microseconds.  Still, this is likely to be the best you can do.

LVL 12

Expert Comment

ID: 8204964
One problem with the gettimeofday() function is that it uses the tv struct which has long for seconds since epoch. That type should probably rather be time_t and time_t better change to 64 bit before 2038 or else you'll be in trouble.


Expert Comment

ID: 8206998
Yeah, well when I was doing Y2K certification stuff, I was also telling colleagues that I'd be retired by 2038, so they better start planning to fix it without me.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question