?
Solved

Getting precision in tiiming code segments

Posted on 2003-03-25
4
Medium Priority
?
148 Views
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.
0
Comment
Question by:dowhiles_donothing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
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 {
      rdtsc
      mov dword ptr x,eax    // move low 32 bits
      mov dword ptr x + 4,edx  // move high 32 bits.
   }
   return x;
}

Alf
0
 
LVL 6

Expert Comment

by:GaryFx
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.

Gary
0
 
LVL 12

Expert Comment

by:Salte
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.

Alf
0
 
LVL 6

Expert Comment

by:GaryFx
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.

Gary
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

762 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