Stop Timer At Any Time and Start Timer Again in C++ -


i doing in c++. example, want calculate time taken process a, reset timer , start timer again calculate time process b. how reset timer , start again second process?

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 a: << time_2 - time_1<< seconds<<endl;      time ( &time_1 );     processb();     time ( &time_2 );      cout<<time taken b: << time_2 - time_1<< seconds<<endl; } 

similar question here:- trying calculate difference between 2 times, won't subtract.

edit: there clock() function approximate measure taken process in execution. whereas time() function gives system time. depends on need, might use 1 of them. these 2 functions return different values. use time() when need exact time taken, including time when process in wait queue resource if need time taken process in execution use clock(). clock() faster (multiple threads executing in parallel speed clock) or slower (resources scarce).

clock code:

it used in same manner time. each call clock returns clock value , reset time

c_start = clock(); processa(); c_end = clock();  cout<<time taken a: << c_end - c_start<< seconds<<endl;  c_start = clock(); processb(); c_end = clock();  cout<<time taken 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 times in previous codes!


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -