### proRam's blog

By proRam, history, 2 years ago,

Can anyone tell me what is the best way to know the execution time of my source code? Best means that the execution of that method to determine time doesn't affect or has minimal effect on the code's actual runtime.

• -1

 » 2 years ago, # |   +3 At the end of your source code you can do something like: #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif 
•  » » 2 years ago, # ^ |   0 I also use this and bdw you don't need to use that #ifdef because the cerr writes to the stderr , not the stdout and codeforces or any other site won't show WA because of cerr statements
•  » » » 2 years ago, # ^ |   +3 I don't do it because: Theoretically, it'll increase run time :P I think it is a bad programming practice to use cerr in that way. Similarly for using cerr for debugging the program in that way. I instead like how the top guys do, they have debug template and use ifdef to print the debug output only locally.
•  » » 2 years ago, # ^ |   -10 This gives different time of execution for the same program if I do it multiple time.
 » 2 years ago, # |   0 You could learn an easy way to do it from your command prompt/terminal. This will determine the overall time taken for the execution of your entire program. Google to learn more about it.Do you want to measure how much time a certain bit of code takes to execute? You could use this simple snippet. Timer (C++)using namespace std; class Timer { private: chrono::time_point Begin, End; public: Timer () : Begin(), End () { Begin = chrono::steady_clock::now(); } ~Timer () { End = chrono::steady_clock::now(); cout << "\nDuration: " << ((chrono::duration )(End - Begin)).count() << "s\n"; } } T; If you declare a Timer object globally, you'll get results similar to what you get from your terminal. If you'd like to measure the performance of some function, declare the Timer object at the beginning of the function.Usually, it's best to put this sort of code inside preprocessor ifdef's etc. For an example of this, you can have a look at my submissions. If you're using recursive functions and want to measure their performance, this doesn't work (but there's a workaround if you know how to use std::function)
 » 2 years ago, # |   0 This std::chrono::system_clock::now().time_since_epoch().count() returns current time in nanosecond. Or you can use custom invocations.
 » 2 years ago, # |   0 Use ideone.com. Make sure that your code is private during contest.
 » 2 years ago, # | ← Rev. 2 →   0 During compiling use this time g++ -std=c++17 cprog.cppThen after execution last line will show: g++ -std=c++17 cprog.cpp 1.31s user 0.10s system 99% cpu 1.415 totalI personally like this over writing some code
•  » » 2 years ago, # ^ |   0 You can have it prewritten so that you don't have to write it again and again in the terminal. Endagorion does the same too.
 » 19 months ago, # |   -16 I know that I'm late but if you add this in your vimrc, you should see execution time. autocmd vimEnter *.cpp map :w :!clear ; g++ --std=c++17 %; if [ -f a.out ]; then time ./a.out; rm a.out; fi