0

Quick C++ timing method to help benchmark code

Function C/C++ 2 revisions 40 24 days ago 24 days ago
void toggleBench() {
	const static std::chrono::high_resolution_clock::time_point zero = std::chrono::system_clock::from_time_t(0);

	static std::chrono::high_resolution_clock::time_point start;

	if (start == zero) {
		start = std::chrono::high_resolution_clock::now();
	} else {
		std::cout
			<< ">> Operation took: "
			<< std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start).count()
			<< "µs"
			<< std::endl;
	}
}

Dependencies

#include <iostream>
#include <chrono>

Usage

toggleBench();
doSomethingInteresting();
toggleBench();

Example output:

>> Operation took: 1582417µs