0

I have the .exe of a program which has been generated from C++.

Is there some simple snippet which I could just insert to get the time taken by the program. I have the C++ code available but I don't want to tweak it much.

Cipher
  • 5,404
  • 19
  • 70
  • 111
  • http://stackoverflow.com/questions/673523/how-to-measure-execution-time-of-command-in-windows-command-line provided several solutions. – timrau Feb 24 '12 at 07:22

3 Answers3

2

Read about Boost.Timers. Code sample to measure time will be:

#include <boost/timer/timer.hpp>

boost::timer t0;
// do smth
std::cout<<"elapsed: "<< t0.elapsed() << " s\n";
CharlesB
  • 75,315
  • 26
  • 174
  • 199
2r2w
  • 1,221
  • 1
  • 11
  • 27
0

A simple way to measure the time taken by some part (or all) of your program is to take a snapshot of the current time at the start and then subtract it from the current time at the end. On Windows, you could use the GetTickCount function for this. I commonly wrap this in a little helper struct:

struct Duration {
  Duration( const char *name )
    : m_start( ::GetTickCount() ),
    m_name( name )
 { }

  ~Duration() {
    std::cout << m_name << " executed in " << ::GetTickCount() - start << "ms" << std::endl;
  }

  const DWORD m_start;
  const std::string m_name;
};

You can use it like this:

int main()
{
  Duration d( "Program" );

  // Heavy work being done here
}

A little timing information is printed to stdout as the Duration object is destroyed.

Frerich Raabe
  • 81,733
  • 18
  • 105
  • 196
  • 1
    It maybe ok for an academic code, but it's very bad practice to add output statements in a destructor, let alone application/computation logic. – Neowizard Feb 24 '12 at 07:32
  • @Neowizard: Absolutely agreed. However, if you just want to figure out how long some parts of your code need to execute (you can use this `Duration` class nicely if you introduce custom scopes to limit the objects lifetime) you don't want a nicely decoupled, modularized, dependency-injection-based profiling framework. You just want a little hack to do the job while you're debugging - it won't end up in the commit anyway. – Frerich Raabe Feb 24 '12 at 08:18
  • I agree. It's a good an simple way to do some running-time related tests/debugging. Just wanted to point out it's not a good production idea – Neowizard Feb 28 '12 at 12:15
0

On unix you would just need to prefix the executable command with "time", and if you by chance have Cygwin installed, then that's what I what suggest to use. Otherwise check Performance Counters, which is the very source of the process performance data on MS platform. It should be possible to do the trick with a pain of one extra method call before the app exit.

bobah
  • 16,722
  • 1
  • 31
  • 57