View Full Version : system clock access in c++
oldmanstan
December 14th, 2005, 07:26 PM
how would i go about accessing the system clock in c++? right now i'm using the clock() and CLOCKS_PER_SEC constant to time how long an operation takes, but it seems that when i run it "nice" the time gets weird. so instead i want to read the system clock at the beginning then at then end and subtract the two for a total time.
any other method that would work would be fine too.
darth_vector
December 14th, 2005, 08:53 PM
you can get the current time by #include'ing <time.h> and doing as follows:
time_t current_time=time(NULL);
...
time_t next_time=time(NULL);
//to calculate difference:
double difference=difftime(current_time,next_time);
toojays
December 15th, 2005, 04:29 AM
I agree with darth_vector's suggestion, but would add that in ISO C++ you should #include <ctime> instead of <time.h>.
thumper
December 15th, 2005, 05:48 AM
I'd take a look at boost::date_time and boost::timer. Much nicer than dealing with time_t.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.