# | Likes | Tech tags | Title | Creator | Created date |
---|---|---|---|---|---|
1 | 0 |
2023-01-28 21:04
|
Measures the running time of a slightly demanding function using std::chrono::steady_clock
#include<iostream>
#include<chrono>
int function_to_measure() {
long long a = 1;
for(int i=0; i<100000000; i++) {
a = (a * 734) % 1000000007;
}
return a;
}
int main() {
using namespace std::chrono;
steady_clock::time_point t = steady_clock::now(); // Create a timepoint before function starts
int result = function_to_measure();
double time_elapsed = duration_cast<duration<double> >(steady_clock::now() - t).count(); // Running time in secs
std::cout << "Took " << time_elapsed << " seconds\n";
std::cout << "Result is " << result << "\n";
return 0;
}
classes | ||
std::chrono::duration |
en.cppreference.com | cplusplus.com |
std::chrono::steady_clock |
en.cppreference.com | cplusplus.com |
std::chrono::time_point |
en.cppreference.com | cplusplus.com |
objects | ||
std::cout |
en.cppreference.com | cplusplus.com |
functions | ||
std::basic_ostream::operator<< |
en.cppreference.com | cplusplus.com |
std::chrono::duration::count |
en.cppreference.com | cplusplus.com |
std::chrono::duration_cast |
en.cppreference.com | cplusplus.com |
std::chrono::steady_clock::now |
en.cppreference.com | cplusplus.com |
Run some demanding code snippet and measure how long it takes to execute.