proRam's blog

By proRam, history, 2 years ago, In English

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.

 
 
 
 
  • Vote: I like it
  • -1
  • Vote: I do not like it

»
2 years ago, # |
  Vote: I like it +3 Vote: I do not like it

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, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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, # ^ |
        Vote: I like it +3 Vote: I do not like it

      I don't do it because:

      1. Theoretically, it'll increase run time :P

      2. 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, # ^ |
      Vote: I like it -10 Vote: I do not like it

    This gives different time of execution for the same program if I do it multiple time.

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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++)

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, # |
  Vote: I like it 0 Vote: I do not like it

This std::chrono::system_clock::now().time_since_epoch().count() returns current time in nanosecond.

Or you can use custom invocations.

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Use ideone.com. Make sure that your code is private during contest.

»
2 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

During compiling use this time g++ -std=c++17 cprog.cpp

Then after execution last line will show:
g++ -std=c++17 cprog.cpp 1.31s user 0.10s system 99% cpu 1.415 total

I personally like this over writing some code

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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, # |
  Vote: I like it -16 Vote: I do not like it

I know that I'm late but if you add this in your vimrc, you should see execution time. autocmd vimEnter *.cpp map <F8> :w <CR> :!clear ; g++ --std=c++17 %; if [ -f a.out ]; then time ./a.out; rm a.out; fi <CR>