Блог пользователя proRam

Автор proRam, история, 4 года назад, По-английски

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
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +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
  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 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

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      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.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

Or you can use custom invocations.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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