简体   繁体   中英

Measure the time taken by the program

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.

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";

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM