Algoteka
Log in to submit your own samples!

Measure Running Time of Code

Problem by oml1111
# Tech tags Title Creator Created date
1 0
2023-01-28 21:04
View all samples for this language (1 verified and 0 unverified)

Using std::chrono::steady_clock | C++ |

By oml1111 |
0 likes

Measures the running time of a slightly demanding function using std::chrono::steady_clock

Code

#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;
}

References

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

Problem Description

Run some demanding code snippet and measure how long it takes to execute.

View sample discussion (0 comments)
View problem discussion (0 comments)