I am doing in C++. For example, I want to calculate the time taken for process A, reset the timer and start the timer again to calculate the time for process B. How do I reset the timer and start it again for my second process?
-
1Instead of manually stop the time for the separate process, have you already looked for any analyze tools? VS2013 prof. for example has an build-in profiler already. An other profiling tool can be "Very Sleepy" or MicroProfiler from MS. – Mr.Yellow Jun 01 '15 at 10:20
1 Answers
Use ctime
library.
Code:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t time_1;
time_t time_2;
time ( &time_1 );
processA();
time ( &time_2 );
cout<<Time Taken by A: << time_2 - time_1<< seconds<<endl;
time ( &time_1 );
processB();
time ( &time_2 );
cout<<Time Taken by B: << time_2 - time_1<< seconds<<endl;
}
Similar question here:- Trying to calculate difference between two times, won't subtract.
Edit: There is clock() function which is an approximate measure taken by a process in execution. Whereas time() function gives you the system time. So depends on what you need, you might use one of them. As these two functions will return different values. Use time() when you need the exact time taken, including the time when the process was in wait queue for resource but if you need time taken by your process in execution then use clock(). clock() will be faster (multiple threads executing in parallel which will speed up the clock) or slower (resources are scarce).
Clock Code:
It is used in the same manner as time. Each call to clock returns the clock value and reset the time
c_start = clock();
processA();
c_end = clock();
cout<<Time Taken by A: << c_end - c_start<< seconds<<endl;
c_start = clock();
processB();
c_end = clock();
cout<<Time Taken by B: << c_end - c_start<< seconds<<endl;
Great reference on clock(): http://en.cppreference.com/w/cpp/chrono/c/clock
Time in milliseconds:
#include <chrono>
using namespace std::chrono;
milliseconds ms = duration_cast< milliseconds >(system_clock::now().time_since_epoch()
Again subract the times as in previous codes!

- 1
- 1

- 5,397
- 8
- 45
- 75
-
How about #include
and clock_t tStart = clock(), is it the same as ctime library? – wkl Jun 01 '15 at 09:31 -
timer.h and ctime are same library. ctime is more like c++ way to access c libraries. Now, clock() is the approximate time taken by the process. It will be different that time function. See my edit. – Mangat Rai Modi Jun 01 '15 at 09:35
-
@LightnessRacesinOrbit Void was my mistake, I corrected. but what is wrong in using clock()? It depends on user need if he needs processing time or real system time. I guess I have clarified the difference well! – Mangat Rai Modi Jun 01 '15 at 09:54
-
Act the code provided by Mangat Rai Modi is runnable. But how do I specify the time in 0.001 s manner? The time shown is in full seconds without decimals. – wkl Jun 01 '15 at 09:59
-
@LightnessRacesinOrbit I have never used clock(), only timer() but if user is interested in approximate execution time as it is evident from the question. I understand the clock might be misleading but it has its uses in many cases – Mangat Rai Modi Jun 01 '15 at 10:01
-
@wkl If you need millisecond time use crono linbrary. See edited answer! – Mangat Rai Modi Jun 01 '15 at 10:04