How can I measure CPU time in C ++ in windows and enable system () calls?

I want to run some tests on the C ++ algorithm and I want to get the processor time it takes, depending on the input. I am using Visual Studio 2012 on Windows 7. I already discovered one way to calculate CPU time on Windows: How can I measure CPU time and wall clock on Linux / Windows?

However, I use system () in my algorithm, which is not measured this way. So, how can I measure the processor time and enable the time of my script calls through system ()?

I have to add a small example. This is my get_cpu_time function (from the link described above):

double get_cpu_time(){ FILETIME a,b,c,d; if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){ // Returns total user time. // Can be tweaked to include kernel times as well. return (double)(d.dwLowDateTime | ((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001; }else{ // Handle error return 0; } } 

This works so far, and when I created the program, it sorts some array (or does some other things that take some time), it works fine. However, when I use the system () command, as in this case, this is not the case:

 int main( int argc, const char* argv[] ) { double start = get_cpu_time(); double end; system("Bla.exe"); end = get_cpu_time(); printf("Everything took %f seconds of CPU time", end - start); std::cin.get(); } 

The execution of this exe file is measured in the same way and takes about 5 seconds. When I run it through system (), all this takes a processor time of 0 seconds, which, obviously, does not include the execution of an exe file.

One possibility would be to get a HANDLE on a system call, is this possible somehow?

+7
c ++ windows
source share
7 answers

Linux:

0
source share

It will actually print out the processor time your program will take. But if you use threads in your program, it will not work properly. You must wait for the thread to finish before choosing the time to finish the processor. So basically you should write this:

 WaitForSingleObject(threadhandle, INFINITE); 

If you don’t know what exactly you are using in your program (if it is multithreaded or not), you can create a thread for this task and wait for the thread to finish and measure the time.

 DWORD WINAPI MyThreadFunction( LPVOID lpParam ); int main() { DWORD dwThreadId; HANDLE hThread; int startcputime, endcputime, wcts, wcte; startcputime = cputime(); hThread = CreateThread( NULL, // default security attributes 0, // use default stack size MyThreadFunction, // thread function name NULL, // argument to thread function 0, // use default creation flags dwThreadIdArray); WaitForSingleObject(hThread, INFINITE); endcputime = cputime(); std::cout << "it took " << endcputime - startcputime << " s of CPU to execute this\n"; return 0; } DWORD WINAPI MyThreadFunction( LPVOID lpParam ) { //do your job here return 0; } 
0
source share

You can try using an accelerator. This is a cross platform way. Sample code from boost website:

 #include <boost/timer/timer.hpp> #include <cmath> int main() { boost::timer::auto_cpu_timer t; for (long i = 0; i < 100000000; ++i) std::sqrt(123.456L); // burn some time return 0; } 
0
source share

If you use C ++ 11 (or have access to it) std::chrono , there are all the functions necessary to calculate the duration of the program.

0
source share

You need to add your process to the Job object before creating child processes. Then the child processes will automatically start in the same task, and the necessary information can be found in the TotalUserTime and TotalKernelTime members of the JOBOBJECT_BASIC_ACCOUNTING_INFORMATION structure, accessible through the QueryInformationJobObject function.

Additional Information:

Starting with Windows 8, nested jobs are supported , so you can use this method even if some of the programs already rely on job objects.

0
source share

I do not think there is a cross-platform mechanism. Using CreateProcess to start the application with the completion of WaitForSingleObject for the application will allow you to get direct descendants. After that, you will need task objects for full accounting (if you need grandchildren time)

0
source share

You can also give profilers selections. I used the free "Sleepy" [ http://sleepy.sourceforge.net/ 022and even better "Very Sleepy" [ http://www.codersnotes.com/sleepy/] Windows profilers and were very pleased with the results - beautifully formatted information in a few minutes with little or no effort.

There is a similar project called "Brilliant" [ http://sourceforge.net/projects/shinyprofiler/] , which should work on both Windows and * nix.

0
source share

All Articles