0

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

Community
  • 1
  • 1
mkerikss
  • 65
  • 6
  • your code won`t pause until the `Execute_some_external_program` has finished. Thats why you can't measure the elapsed time. you'd need something to flag a rdy-state – Ben Win Sep 11 '14 at 07:21
  • Can you elaborate on this? – mkerikss Sep 11 '14 at 07:35
  • Sorry but i just failed to read right. System normaly should be blocking. – Ben Win Sep 11 '14 at 07:47
  • So i guess calling `system( Execute_some_external_program.c_str() );` in another thread might solve your problem. Since `system` blocks your program, `cpu_timer timer;` might be blocked aswell – Ben Win Sep 11 '14 at 07:51
  • I think either I'm not understanding you correctly or it's the other way around. The problem is not that system blocks my program or the cpu_timer, but rather that cpu_timer performs it's measurements with respect to my program, and not with respect to some_external_program which is what I want to measure. – mkerikss Sep 11 '14 at 08:03

0 Answers0