Skip to content

Instantly share code, notes, and snippets.

@zorchp
Created September 15, 2025 06:48
Show Gist options
  • Save zorchp/8f2552ea3dc765f66269bac4c81fa6e7 to your computer and use it in GitHub Desktop.
Save zorchp/8f2552ea3dc765f66269bac4c81fa6e7 to your computer and use it in GitHub Desktop.
#include <random>
#include <chrono>
#include <iostream>
using namespace std::chrono;
class TimeCost {
public:
TimeCost() : _start_time(std::chrono::high_resolution_clock::now()) {}
void begin_time_point() {
_start_time = std::chrono::high_resolution_clock::now();
}
double get_time_cost(bool is_reset = false) {
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
end_time - _start_time);
double cost = static_cast<double>(duration.count()) / 1e3;
if (is_reset) {
_start_time = std::chrono::high_resolution_clock::now();
}
return cost;
}
private:
std::chrono::high_resolution_clock::time_point _start_time;
};
int main() {
TimeCost tc;
cout << tc.get_time_cost() << "ms\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment