Skip to content

Instantly share code, notes, and snippets.

@cedarz
Forked from Changes729/chrono_gettime.cpp
Last active February 28, 2024 09:13
Show Gist options
  • Select an option

  • Save cedarz/ae433ee1790bca4f94c85384306f2c0a to your computer and use it in GitHub Desktop.

Select an option

Save cedarz/ae433ee1790bca4f94c85384306f2c0a to your computer and use it in GitHub Desktop.

Revisions

  1. cedarz revised this gist Feb 28, 2024. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions timer.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    class ScopedTimer
    {
    public:
    ScopedTimer(const std::string& name = "ScopeTimer") : eventName(name)
    {
    start = std::chrono::system_clock::now();
    }

    ~ScopedTimer()
    {
    auto end = std::chrono::system_clock::now();
    std::cout << eventName << " : " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
    }

    private:
    std::string eventName;
    std::chrono::system_clock::time_point start;
    };
  2. @Changes729 Changes729 revised this gist Nov 30, 2020. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  3. @Changes729 Changes729 revised this gist Nov 27, 2020. 2 changed files with 40 additions and 27 deletions.
    29 changes: 17 additions & 12 deletions Test one function's run time.
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,21 @@
    struct timespec start, end;
    clock_gettime(CLOCK_MONOTONIC, &start);
    #include <time.h>

    // test...
    int main(void)
    {
    struct timespec start, end;
    clock_gettime(CLOCK_MONOTONIC, &start);

    clock_gettime(CLOCK_MONOTONIC, &end);
    // test...

    // print
    // Calculating total time taken by the program.
    double time_taken;
    time_taken = (end.tv_sec - start.tv_sec) * 1e9;
    time_taken = (time_taken + (end.tv_nsec - start.tv_nsec)) * 1e-9;
    clock_gettime(CLOCK_MONOTONIC, &end);

    cout << "Time taken by program is : " << fixed << setprecision(9)
    << time_taken;
    cout << " sec" << endl;
    // print
    // Calculating total time taken by the program.
    double time_taken;
    time_taken = (end.tv_sec - start.tv_sec) * 1e9;
    time_taken = (time_taken + (end.tv_nsec - start.tv_nsec)) * 1e-9;

    cout << "Time taken by program is : " << fixed << setprecision(9)
    << time_taken;
    cout << " sec" << endl;
    }
    38 changes: 23 additions & 15 deletions [ Example ] C++ run time
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,29 @@
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);
    #include <iostream>
    #include <chrono>

    // auto start = chrono::system_clock::now();
    auto start = chrono::steady_clock::now();
    using namespace std;

    func();
    int main(void)
    {
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);

    // please do not use high_resolution_clock. see: https://zh.cppreference.com/w/cpp/chrono/high_resolution_clock
    // auto end = chrono::system_clock::now();
    auto end = chrono::steady_clock::now();
    // auto start = chrono::system_clock::now();
    auto start = chrono::steady_clock::now();

    // Calculating total time taken by the program.
    double time_taken
    = chrono::duration_cast<chrono::nanoseconds>(end - start).count();
    func();

    time_taken *= 1e-9;
    // please do not use high_resolution_clock. see: https://zh.cppreference.com/w/cpp/chrono/high_resolution_clock
    // auto end = chrono::system_clock::now();
    auto end = chrono::steady_clock::now();

    cout << "Time taken by program is : " << fixed << setprecision(9)
    << time_taken;
    cout << " sec" << endl;
    // Calculating total time taken by the program.
    double time_taken
    = chrono::duration_cast<chrono::nanoseconds>(end - start).count();

    time_taken *= 1e-9;

    cout << "Time taken by program is : " << fixed << setprecision(9)
    << time_taken;
    cout << " sec" << endl;
    }
  4. @Changes729 Changes729 revised this gist Nov 27, 2020. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions [ Example ] C++ run time
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);

    // auto start = chrono::system_clock::now();
    auto start = chrono::steady_clock::now();

    func();

    // please do not use high_resolution_clock. see: https://zh.cppreference.com/w/cpp/chrono/high_resolution_clock
    // auto end = chrono::system_clock::now();
    auto end = chrono::steady_clock::now();

    // Calculating total time taken by the program.
    double time_taken
    = chrono::duration_cast<chrono::nanoseconds>(end - start).count();

    time_taken *= 1e-9;

    cout << "Time taken by program is : " << fixed << setprecision(9)
    << time_taken;
    cout << " sec" << endl;
  5. @Changes729 Changes729 created this gist Nov 26, 2020.
    16 changes: 16 additions & 0 deletions Test one function's run time.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    struct timespec start, end;
    clock_gettime(CLOCK_MONOTONIC, &start);

    // test...

    clock_gettime(CLOCK_MONOTONIC, &end);

    // print
    // Calculating total time taken by the program.
    double time_taken;
    time_taken = (end.tv_sec - start.tv_sec) * 1e9;
    time_taken = (time_taken + (end.tv_nsec - start.tv_nsec)) * 1e-9;

    cout << "Time taken by program is : " << fixed << setprecision(9)
    << time_taken;
    cout << " sec" << endl;
    41 changes: 41 additions & 0 deletions [ Example ] clock_gettime
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    /* int clock_gettime( clockid_t clock_id, struct
    timespec *tp ); The clock_gettime() function gets
    the current time of the clock specified by clock_id,
    and puts it into the buffer pointed to by tp.tp
    parameter points to a structure containing
    atleast the following members:
    struct timespec {
    time_t tv_sec; // seconds
    long tv_nsec; // nanoseconds
    };
    clock id = CLOCK_REALTIME, CLOCK_PROCESS_CPUTIME_ID,
    CLOCK_MONOTONIC ...etc
    CLOCK_REALTIME : clock that measures real (i.e., wall-clock) time.
    CLOCK_PROCESS_CPUTIME_ID : High-resolution per-process timer
    from the CPU.
    CLOCK_MONOTONIC : High resolution timer that is unaffected
    by system date changes (e.g. NTP daemons). */
    struct timespec start, end;

    // start timer.
    // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
    // clock_gettime(CLOCK_REALTIME, &start);
    clock_gettime(CLOCK_MONOTONIC, &start);

    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);

    func();
    // stop timer.
    // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
    // clock_gettime(CLOCK_REALTIME, &end);
    clock_gettime(CLOCK_MONOTONIC, &end);

    // Calculating total time taken by the program.
    double time_taken;
    time_taken = (end.tv_sec - start.tv_sec) * 1e9;
    time_taken = (time_taken + (end.tv_nsec - start.tv_nsec)) * 1e-9;

    cout << "Time taken by program is : " << fixed << setprecision(9)
    << time_taken;
    cout << " sec" << endl;