Last active
December 2, 2024 15:16
-
-
Save kristopherjohnson/c52f3b838400f71894753f1658a20298 to your computer and use it in GitHub Desktop.
C++ stopwatch, for calculating elapsed time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of using the Stopwatch class to measure elapsed time. | |
#include <iostream> | |
#include <thread> | |
#include "stopwatch.h" | |
int main(int argc, const char **argv) { | |
// Construct stopwatch starting at current time. | |
Stopwatch stopwatch; | |
// Do something that takes time. | |
std::this_thread::sleep_for(std::chrono::milliseconds(1500)); | |
// Mark the end time. | |
stopwatch.mark(); | |
std::cout << "Elapsed seconds: " << stopwatch.elapsed_sec() << std::endl; | |
std::cout << "Elapsed milliseconds: " << stopwatch.elapsed_milli() | |
<< std::endl; | |
std::cout << "Elapsed microseconds: " << stopwatch.elapsed_micro() | |
<< std::endl; | |
std::cout << "Elapsed nanoseconds: " << stopwatch.elapsed_nano() << std::endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STOPWATCH_H | |
#define STOPWATCH_H | |
#include <chrono> | |
/// Calculates elapsed time between start and mark, using high-resolution clock. | |
/// | |
/// Note: It is not safe to call functions of a Stopwatch instance from multiple | |
/// threads withuot explicit synchronization. | |
class Stopwatch { | |
public: | |
using clock = std::chrono::steady_clock; | |
using time_point = std::chrono::time_point<clock>; | |
/// Create stopwatch with current time as start time. | |
Stopwatch() { start_time = mark_time = clock::now(); } | |
/// Create stopwatch with specified start and mark times. | |
Stopwatch(time_point start, time_point mark) | |
: start_time(start), mark_time(mark) {} | |
/// Set the start time to the current instant. | |
void start() { start_time = clock::now(); } | |
/// Set the mark time to the current instant. | |
void mark() { mark_time = clock::now(); } | |
time_point get_start() const { return start_time; } | |
time_point get_mark() const { return mark_time; } | |
/// Return the interval between start and mark in seconds. | |
double elapsed_sec() const { | |
return std::chrono::duration_cast<std::chrono::duration<double>>(mark_time - | |
start_time) | |
.count(); | |
} | |
/// Return the interval between start and mark in milliseconds. | |
double elapsed_milli() const { | |
return std::chrono::duration_cast< | |
std::chrono::duration<double, std::milli>>(mark_time - | |
start_time) | |
.count(); | |
} | |
/// Return the interval between start and mark in microseconds. | |
double elapsed_micro() const { | |
return std::chrono::duration_cast< | |
std::chrono::duration<double, std::micro>>(mark_time - | |
start_time) | |
.count(); | |
} | |
/// Return the interval between start and mark in nanoseconds. | |
double elapsed_nano() const { | |
return std::chrono::duration_cast<std::chrono::duration<double, std::nano>>( | |
mark_time - start_time) | |
.count(); | |
} | |
private: | |
time_point start_time; | |
time_point mark_time; | |
}; | |
#endif // STOPWATCH_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment