Created
March 8, 2022 07:07
-
-
Save junaire/c1d296f1f6076a101019d3c423442066 to your computer and use it in GitHub Desktop.
c/c++ program time-consuming
This file contains hidden or 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
#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