Using Boost's timer (or any other timer) on windows in c++ to measure CPU time of external call to system

552 views Asked by At

I've spent the last three days trying to find a solution to the following problem: I have a piece of code written in c++ in Microsoft Visual Studio 2010 from which I call some external executables, which are blackboxed to me, so I can't change their code at all. I'd like to know the CPU-time(as opposed to the elapsed time/wall clock time) of these external programs. In Linux, this would be solved simply by using the time command, followed by the call to the executable, but in Windows 2007 I can't find a ny solution.

I thought boost's timer class would solve my problem, but as it turns out, it can't measure the CPU-times of the external program, only of the program in which it is actually implemented. At least I think so. This is the relevant part of my code.

    using boost::timer::cpu_timer;
    using boost::timer::cpu_times;
    using boost::timer::nanosecond_type;

    cpu_timer timer;        
    system( Execute_some_external_program.c_str() );        
    cpu_times elapsed_times = timer.elapsed();

    std::cout << "Wall: " << elapsed_times.wall/1.0e9 << "\tCPU: " << \
    (elapsed_times.user + elapsed_times.system)/1.0e9 << std::endl;

(The divide by 1e9 is since times are given in nanoseconds). The output I get is:

Wall: 120.101 CPU: 0.0156001

So no CPU-time has been measured for the whole time the external program has been executed, only wall clock time.

So, how can I measure the CPU-times of external programs in a framework like this? I really need the CPU-times, since some of the externals in their current implementation use multi-threading and some don't, and I want to be able to make a comparison of computational times as if all used single-threading (I can't force the programs to use single-threading at the moment).

EDIT: This link contains some useful information about this problem. How can I measure CPU time in C++ on windows and include calls of system()? however, I'm not really a programmer so using handles and stuff scares me a bit :P I have looked into it and even tried it but didn't get it to work, so if that is the only way, I definitely need a thorough explanation of how to actually do that.

Thank you in advance for your help!

Best regards, Mikael

0

There are 0 answers