Intel Threading Building Blocks provides a thread-safe and portable method to compute elapsed time.
Many operating systems provide a fine-grained method to track the accounting of central processing unit (CPU) time, and most provide a way to track elapsed/wall-clock time. The method of extracting an elapsed measurement with subsecond accuracy varies a great deal.
The class tick_count
in Threading Building Blocks provides a simple interface for measuring wall-clock time, as shown in Example 8-1.
Example 8-1. Using tick_count to measure elapsed time
#include "tbb/tick_count.h" using namespace tbb; ... tick_count t0 = tick_count::now(); ... do some work ... tick_count t1 = tick_count::now(); printf("work took %g seconds ",(t1-t0).seconds());
This is guaranteed to work even if the processor core running a task changes while the work is being done, and therefore the tick_count
calls run on different processors.
Unlike some timing interfaces, tick_count
is guaranteed to be safe to use across threads. It is based on a common or global clock. It is valid to subtract tick_count
values that were created by different threads to compute elapsed time.
A tick_count
value obtained from the static method tick_count::now()
represents the current absolute time. This value has no meaning other than for use in comparisons with other tick_count
values. Subtracting two tick_count
values yields a relative time in tick_count::interval_t
, as shown in Example 8-1. Relative time is expressed in seconds as a double
in order to allow fractional values in keeping with the resolution available on the operating system.
The resolution of tick_count
corresponds to the highest-resolution timing service on the platform that is valid across threads in the same process. The routines are wrappers around operating system services that have been verified as safe to use across threads.
3.14.6.194