Skip to content

Instantly share code, notes, and snippets.

@junaire
Created March 8, 2022 07:07
Show Gist options
  • Save junaire/c1d296f1f6076a101019d3c423442066 to your computer and use it in GitHub Desktop.
Save junaire/c1d296f1f6076a101019d3c423442066 to your computer and use it in GitHub Desktop.
c/c++ program time-consuming
#include <chrono>
#include <iostream>
class Timer {
public:
Timer() : start_(std::chrono::high_resolution_clock::now()){};
~Timer() {
auto end = std::chrono::high_resolution_clock::now();
auto s = std::chrono::time_point_cast<std::chrono::microseconds>(start_)
.time_since_epoch()
.count();
auto e = std::chrono::time_point_cast<std::chrono::microseconds>(end)
.time_since_epoch()
.count();
auto dura = e - s;
std::cout << "Duration: " << dura << "us(" << dura * 0.001 << " ms)\n";
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> start_;
};
int main() {
Timer timer;
int res = 0;
for (int i = 0; i < 10000; ++i) {
res += i * 2;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment