Chapter 8. Timing

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\n",(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.

Tip

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 ...

Get Intel Threading Building Blocks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.