-
-
Save cedarz/ae433ee1790bca4f94c85384306f2c0a to your computer and use it in GitHub Desktop.
Revisions
-
cedarz revised this gist
Feb 28, 2024 . 1 changed file with 18 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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; }; -
Changes729 revised this gist
Nov 30, 2020 . 3 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes. -
Changes729 revised this gist
Nov 27, 2020 . 2 changed files with 40 additions and 27 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,21 @@ #include <time.h> int main(void) { 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; } 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 charactersOriginal file line number Diff line number Diff line change @@ -1,21 +1,29 @@ #include <iostream> #include <chrono> using namespace std; int main(void) { // 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; } -
Changes729 revised this gist
Nov 27, 2020 . 1 changed file with 21 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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; -
Changes729 created this gist
Nov 26, 2020 .There are no files selected for viewing
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 charactersOriginal 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; 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 charactersOriginal 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;