Synchronization algorithm: clock () vs time () in C ++

To synchronize the algorithm (approximately in ms), which of these two approaches is better:

clock_t start = clock(); algorithm(); clock_t end = clock(); double time = (double) (end-start) / CLOCKS_PER_SEC * 1000.0; 

Or,

 time_t start = time(0); algorithm(); time_t end = time(0); double time = difftime(end, start) * 1000.0; 

In addition, from some discussions on the C ++ channel in Freenode, I know that the clock has a very poor resolution, so the time will be zero for a (relatively) fast algorithm. But, who has the best resolution time () or clock ()? Or is it the same thing?

+45
c ++ timing clock
Sep 01 '12 at 20:33
source share
5 answers

It depends on what you want: time measures the real time, and clock measures the processing time of the current process. If your process falls asleep for any noticeable time, or the system is busy with other processes, they will be very different.

http://en.cppreference.com/w/cpp/chrono/c/clock

+34
Sep 01
source share

<chrono> would be the best library if you use C ++ 11.

 #include <iostream> #include <chrono> #include <thread> void f() { std::this_thread::sleep_for(std::chrono::seconds(1)); } int main() { auto t1 = std::chrono::high_resolution_clock::now(); f(); auto t2 = std::chrono::high_resolution_clock::now(); std::cout << "f() took " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << " milliseconds\n"; } 

An example taken from here .

+36
Sep 01
source share

The time_t structure is likely to be an integer, which means that it will have a second resolution.

The first piece of code: it will only count the time when the processor did something, so when you sleep (), it won’t count anything. You can get around it by calculating your sleep time (), but after a while it will probably start to drift.

The second part: only the resolution of seconds, not so large if you need a subsecond time indication.

To show the time with the best resolution, you can get something like this:

 double getUnixTime(void) { struct timespec tv; if(clock_gettime(CLOCK_REALTIME, &tv) != 0) return 0; return (tv.tv_sec + (tv.tv_nsec / 1000000000.0)); } double start_time = getUnixTime(); double stop_time, difference; doYourStuff(); stop_time = getUnixTime(); difference = stop_time - start_time; 

In most systems, the resolution will be up to several microseconds, but it can vary depending on different processors and, possibly, even on the main kernel versions.

+8
Sep 01
source share

<chrono> is the best. Visual Studio 2013 provides this feature. Personally, I tried all the methods mentioned above. I highly recommend you use the <chrono> library. It can track wall time and at the same time have a good resolution (much less than a second).

+1
Dec 08 '14 at 1:46
source share

How about gettimeofday? When it is called, it updates two structures with time information. Usually there is enough left structure that will carry time since the era, 01-01-1970 00:00:00 (UTC). It can be used as follows:

 #include <time.h> struct timeval start; double mtime, seconds, useconds; gettimeofday(&start, NULL); //left hand struct is usually enough seconds = start.tv_sec; //time in seconds useconds = start.tv_usec; //time in microseconds 
0
Jun 01 '15 at 9:16
source share



All Articles