camino
camino

Reputation: 10584

what is the best way to test the performance of a program in linux

Suppose I write a program, then I make some "optimization" for the code. In my case I want to test how much the new feature std::move in C++11 can improve the performance of a program. I would like to check whether the "optimization" do make sense.

Currently I test it by the following steps:

  1. write a program(without std::move) , compile ,get binary file m1
  2. optimize it(using std::move), compile, get binary file m2
  3. use command "time" to compare the time consuming:

    time ./m1 ; time ./m2
    

EDITED:

In order to get the statistical result, it was needed to run the test thousands of times.

Is there any better ways to do that or is there some tools can help on it ?

Upvotes: 2

Views: 668

Answers (2)

Pixelchemist
Pixelchemist

Reputation: 24936

There are several tools (among others) that can do profiling for you:

Some of them require a specific way of compilation or a specific compiler. Some of them are specifically good at profiling for a given processor-architecture (AMD/Intel...).

Since you seem to have access to C++11 and if you just want to measure some timings you can use std::chrono.

#include <chrono>
#include <iostream>
class high_resolution_timer
{
private:
  typedef std::chrono::high_resolution_clock clock;
  clock::time_point m_time_point;
public:
  high_resolution_timer (void) 
    : m_time_point(clock::now()) { }
  void restart (void) 
  { 
    m_time_point = clock::now(); 
  }
  template<class Duration>
  Duration stopover (void) 
  { 
    return std::chrono::duration_cast<Duration>
           (clock::now()-m_time_point);  
  }
}; 

int main (void)
{
  using std::chrono::microseconds;
  high_resolution_timer timer;

  // do stuff here

  microseconds first_result = timer.stopover<microseconds>();
  timer.restart();

  // do other stuff here

  microseconds second_result = timer.stopover<microseconds>();

  std::cout << "First took " << first_result.count() << " x 10^-6;";
  std::cout << " second took " << second_result.count() << " x 10^-6.";
  std::cout << std::endl;

}

But you should be aware that there's almost no sense in optimizing several milliseconds of overall runtime (if your program runtime will be >= 1s). You should instead time highly repetitive events in your code (if there are any, or at least those which are the bottlenecks). If those improve significantly (and this can be in terms of milli or microseconds) your overall performance will likely increase, too.

Upvotes: 0

Felix Glas
Felix Glas

Reputation: 15524

In general measuring performance using a simple time comparison, e.g. endTime-beginTime is always a good start, for a rough estimation.

Later on you can use a profiler, like Valgrind to get measures of how different parts of your program is performing.

With profiling you can measure space (memory) or time complexity of a program, usage of particular instructions or frequency/duration of function calls.

There's also AMD CodeAnalyst if you want more advanced profiling functionality using a GUI. It's free/open source.

Upvotes: 1

Related Questions