proRam's blog

By proRam, history, 5 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

| Write comment?
»
5 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
  • »
    »
    5 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

    • »
      »
      »
      5 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.

»
5 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.

»
5 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.

»
5 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

  • »
    »
    5 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.