Question

Normally in an IDE, when you run a program the IDE will tell you the total amount of time that it took to run the program. Is there a way to get the total amount of time that it takes to run a program when using the terminal in Unix/Linux to compile and run?

I'm aware of ctime which allows for getting the total time since 1970, however I want to get just the time that it takes for the program to run.

Was it helpful?

Solution

You can start programs with time:

[:~/tmp] $ time sleep 1

real    0m1.007s
user    0m0.001s
sys     0m0.003s

OTHER TIPS

You are on the right track! You can get the current time and subtract it from the end time of your program. The code below illustrated:

time_t begin = time(0);   // get current time

// Do Stuff //

time_t end = time(0);   // get current time

// Show number of seconds that have passed since program began //
std::cout << end - begin << std::endl;

NOTE: The time granularity is only a single second. If you need higher granularity, I suggest looking into precision timers such as QueryPerformanceCounter() on windows or clock_gettime() on linux. In both cases, the code will likely work very similarly.

As an addendum to mdsl's answer, if you want to get something close to that measurement in the program itself, you can get the time at the start of the program and get the time at the end of the program (as you said, in time since 1970) - then subtract the start time from the end time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top