2

I'd like a way to measure C++ code as accurately as possible. I did see boost has a high resolution clock but the boost documentation says it's an expensive clock in terms of performance- kinda defeating the purpose:

http://www.boost.org/doc/libs/1_47_0/doc/html/chrono/users_guide.html

What other ways are there? Is there any way to measure in CPU cycles??

intrigued_66
  • 16,082
  • 51
  • 118
  • 189
  • 1
    [Boost.Stopwatches](https://svn.boost.org/trac/boost/wiki/ReviewScheduleLibraries#Boost.Stopwatches) is pending review for inclusion in Boost. That will probably become the best option. – R. Martinho Fernandes Dec 06 '12 at 14:31
  • 1
    What is it you want to measure? Code execution time? For what purpose? – johannes Dec 06 '12 at 14:31
  • Would a profiler suit your needs ? – Sander De Dycker Dec 06 '12 at 14:31
  • The "classic" solution is to check the difference between to calls to the `clock` function. – Some programmer dude Dec 06 '12 at 14:34
  • @SanderDeDycker probably but I just wanted to know for a smaller scale, excluding profilers, is there a c++ code solution? – intrigued_66 Dec 06 '12 at 14:35
  • 1
    Do you want to measure the elapsed (real) time or the CPU usage time? Note that the elapsed time isn't accurate when you run other tasks on the same machine, while the CPU usage time should be more accurate in this case (at least in theory). – leemes Dec 06 '12 at 14:42
  • @Everyone, ideally I would like to count CPU cycles without having to use a profiler- either using OS calls or C++ code? – intrigued_66 Dec 07 '12 at 09:12

4 Answers4

2

If you are using GCC, try GNU profiler (gprof) http://sourceware.org/binutils/docs/gprof/. Gprof probably gives you the most accurate results.

Taking the difference of start/end time in a function is less accurate to due context switches (b/w process or threads).

Kiran Mohan
  • 2,696
  • 6
  • 36
  • 62
1

On Windows use QueryPerformanceCounter().

On Solaris use gethrtime().

On other UNIX use gettimeofday().

Of course you should wrap them in a class so your code only uses the class and not the methods directly.

We use a class like

class dbbHighResTimer {
public:
    dbbHighResTimer(); // Does call set()
    dbbHighResTimer( int ); // Does not call set()
    void    set();
    friend double operator - ( const dbbHighResTimer &, const dbbHighResTimer & );
    friend int compare( const dbbHighResTimer &, const dbbHighResTimer & );
};
brian beuning
  • 2,836
  • 18
  • 22
1

If this is for profiling on Windows, use rdtsc instruction, provided it is accurate on your CPU (see How to detect if RDTSC returns a constant rate counter value? for a discussion). It is a lot faster than QueryPerformanceCounter.

Community
  • 1
  • 1
Suma
  • 33,181
  • 16
  • 123
  • 191
1

I can't tell from your question if you want external or internal profiling.

For external profiling, you have Quantify (or its successor) on Windows, and gprof on any unix with g++ and friends. If you use Solaris you even have dtrace, which you can attach to a running application to do statistical profiling.

If you're looking for internal profiling, gettimeofday on Solaris/Linux should be quite performant enough for anything except inside a tight loop.

Mark B
  • 95,107
  • 10
  • 109
  • 188