Skip to content

Clock

Clock

A clock consists of a starting point (or epoch) and a tick rate. For example, a clock may have an epoch of January 1, 1970 and tick every second. C++ defines several clock types:

  • system_clock (C++11): wall clock time from the system-wide realtime clock
  • steady_clock (C++11): monotonic clock that will never be adjusted
  • high_resolution_clock (C++11): the clock with the shortest tick period available
  • utc_clock (C++20): Clock for Coordinated Universal Time (UTC)
  • tai_clock (C++20): Clock for International Atomic Time (TAI)
  • gps_clock (C++20): Clock for GPS time
  • file_clock (C++20): Clock used for file time
  • local_t (C++20): pseudo-clock representing local time

C++ also inherits the C clock function, which returns the approximate processor time used by the process since the beginning of an implementation-defined era related to the program's execution. To convert result value to seconds divide it by CLOCKS_PER_SEC. The clock function:

  • The only method specified in the standard to measure CPU time
  • It's up to the user to keep track of the duration unit
  • It doesn't work well with threads
  • Ignores the time spent on this_thread::sleep_for
1
2
3
4
5
6
7
clock_t start, end;
double cpu_time_used;
start = clock();
very_expensive_function();
end = clock();
cpu_time_used = static_cast<float>(end - start) / CLOCKS_PER_SEC;
std::cout << "cpu_time_used: " << cpu_time_used << " seconds" << '\n';

1
2
3
auto start2 = std::chrono::system_clock::now();
very_expensive_function();
auto end2 = std::chrono::system_clock::now();

1
2
3
auto auto_duration = end2 - start2;
std::cout << "auto_duration.count() : " << auto_duration.count() << " ticks"
          << '\n'; // usually nanoseconds

1
2
3
std::chrono::duration<double> seconds_as_double = end2 - start2;
std::cout << "seconds_as_double.count() : " << seconds_as_double.count()
          << " seconds" << '\n';

1
std::chrono::duration<int, std::milli> milliseconds_as_int_;

1
2
3
4
5
milliseconds_as_int_ =
    std::chrono::duration_cast<std::chrono::duration<int, std::milli>>(
        seconds_as_double);
std::cout << "milliseconds_as_int_.count() : "
          << milliseconds_as_int_.count() << " milliseconds" << '\n';

1
2
3
4
5
6
7
8
// Same as using hours
constexpr int seconds_per_hour = 60 * 60;
std::chrono::duration<int, std::ratio<seconds_per_hour>> hours_as_int;
hours_as_int =
    std::chrono::duration_cast<std::chrono::duration<int, std::ratio<60 * 60>>>(
        seconds_as_double);
std::cout << "hours_as_int.count() : " << hours_as_int.count() << " hours"
          << '\n';

1
2
3
auto start3 = std::chrono::steady_clock::now();
very_expensive_function();
auto end3 = std::chrono::steady_clock::now();

1
2
3
auto d3 = end2 - start2;
std::cout << "auto_duration_3.count() : " << d3.count()
          << " ticks" << '\n'; // usually nanoseconds

1
2
3
std::chrono::duration<double> seconds_as_double_3 = end3 - start3;
std::cout << "seconds_as_double_3.count() : " << seconds_as_double_3.count()
          << " seconds" << '\n';

1
2
3
4
5
6
std::chrono::duration<int, std::milli> milliseconds_as_int_3;
milliseconds_as_int_3 =
    std::chrono::duration_cast<std::chrono::duration<int, std::milli>>(
        seconds_as_double_3);
std::cout << "milliseconds_as_int_3.count() : "
          << milliseconds_as_int_3.count() << " milliseconds" << '\n';

1
2
3
4
5
6
7
std::chrono::duration<int, std::ratio<seconds_per_hour>>
    hours_as_int3; // same as using std::hours
hours_as_int3 = std::chrono::duration_cast<
    std::chrono::duration<int, std::ratio<seconds_per_hour>>>(
    seconds_as_double_3);
std::cout << "hours_as_int3.count() : " << hours_as_int3.count() << " hours"
          << '\n';

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// While std::std::chrono::parse is not available in all main compilers
auto hours =
    std::chrono::duration_cast<std::chrono::hours>(d3);
if (hours.count() > 0) {
    std::cout << hours.count() << " hours";
}
d3 -= hours;
auto seconds =
    std::chrono::duration_cast<std::chrono::seconds>(d3);
if (seconds.count() > 0) {
    std::cout << seconds.count() << " seconds";
}
d3 -= seconds;
auto milliseconds =
    std::chrono::duration_cast<std::chrono::milliseconds>(d3);
if (milliseconds.count() > 0) {
    std::cout << milliseconds.count() << " milliseconds";
}
d3 -= milliseconds;
auto nanoseconds =
    std::chrono::duration_cast<std::chrono::nanoseconds>(d3);
if (nanoseconds.count() > 0) {
    std::cout << nanoseconds.count() << " nanoseconds";
}

Share Snippets