1

I have the following C++ code (I have simplified it for the sake of simplicity.)

int main()
{
     string runCommand="./runAnotherCppProgram.out";
     for (int i=0; i<5; i++)
     {
       system(runCommand.c_str());
     }
     return 0;
}

Now I want to know how much time does runAnotherCppProgram.out take at each iteration. To do this I did the following thing:

int main()
{
     string runCommand = "./runAnotherCppProgram.out";
     for (int i=0; i<5; i++)
     {
       clock_t clockStart = clock();
       system(runCommand.c_str());
       double finish = (double)(clock() - clockStart)/CLOCKS_PER_SEC;
       cout << finish << endl;
     }
    return 0;
}

However, it gives much less then the real time of executing. What might be the problem?

3
  • if you are on linux/cygwin/msys use time anotherprogram to get this on shell and cross verify Commented May 20, 2015 at 16:10
  • @tejashs yes I did your suggestion already. Results from time anotherProgram shows 1m123.215s but the output coming from cout prints 0.000267 for example Commented May 20, 2015 at 16:11
  • Generally speaking, it would be nice if you @toshibanexus78 could mark the answer to your question as an answer (accept as answer) so that other users with the same problem don't have to read through all the replies and links. Commented May 27, 2015 at 13:21

3 Answers 3

2

If you have access to C++11....

#include <chrono>

and

auto start = std::chrono::steady_clock::now();

//DO SOMETHING HERE

auto stop = std::chrono::steady_clock::now();

auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop-start).count();

std::cout << "Running time was: " << ((double)duration / 1000.0) << "s" << std::endl;

You can play around with the units you cast it to and how you want to display it... (aka I did milliseconds and then divided by 1000 to get seconds with some decimal points, duration comes out as an integer so it will be rounded if you just ask for seconds)

Sign up to request clarification or add additional context in comments.

Comments

1

Carefully read the documentation of clock(), it only reports time for the process calling it. With system() you start a shell though (and in turn another program from there) which runs in a different process.

BTW: Search the web for "timing C++" or something like that, it should turn up sufficient amounts of results, e.g. time() or Boost.Chrono.

2 Comments

@Ulrich_Eckhardt thanks for the information, I get it know, do you have any solution how could I learn running time of a program inside another program.
Added according suggestion for locating a solution.
1

From Measuring execution time of a call to system() in C++

Have you considered using gettimeofday?

struct timeval tv;
struct timeval start_tv;

gettimeofday(&start_tv, NULL);

system(something);

double elapsed = 0.0;

gettimeofday(&tv, NULL);
elapsed = (tv.tv_sec - start_tv.tv_sec) +
(tv.tv_usec - start_tv.tv_usec) / 1000000.0;

3 Comments

does thise piece of code gives the elapsed time as seconds?
yes, the difference between end time and start time is in micro seconds (10^-6 -> 10^6 micro seconds in a second), divide that difference by 1000000 (10^6) and you'll get the seconds!
did this answer solve your problem? If so, please consider marking it as an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.