CodingKnight's blog

By CodingKnight, 4 years ago, In English

Hello Codeforces,

It is sometimes required in competitive programming problems at run-time to measure elapsed time between two events on a real-time scale such as milliseconds. It is possible in C++ to do that using the std::chrono library classes and functions. The following is a C++ timer class that simplifies doing this operation.

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

class timer: high_resolution_clock {
    const time_point start_time;
public:
    timer(): start_time(now()) {}
    rep elapsed_time() const { return duration_cast<milliseconds>(now()-start_time).count(); } };

The timer class inherits the base class std::chrono::high_resolution_clock. A private std::chrono::time_point constant variable start_time is used to store the initial point of time at which the timer object was created using the class constructor function. The class features a constant public member function elapsed_time() whose call returns a non-negative integer which measures the elapsed time in milliseconds since the creation of the timer object.

The following is a sample example for using the timer class.

const int T = 1000;
timer t; // create a timer object

int main() {
    while (t.elapsed_time() < T) {
        // do something
    }
}

Your constructive comments and feedback are welcome and appreciated.

  • Vote: I like it
  • +3
  • Vote: I do not like it

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

How can it help in a contest?

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

    The while loop in the sample program stops when elapsed time is larger than or equal to T. Suppose that T is set to a value that is slightly smaller than the time-limit for the problem. It should be then possible to guarantee that the submitted solution does not get TLE (Time-Limit Exceed) Verdict.

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it +1 Vote: I do not like it

      How can you guarantee that? Let us suppose on some iteration t.elapsed_time() returned value very close to T, the loop proceeded and resulted in... TLE :)

      Edit: I have just remembered my co-worker argued one day that passed tests == no bugs in a program ;)

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

        It should be guranteed that TLE is avoided if the upper-bound on the execution time of a single iteration is known, and is less than the time-limit of the problem.

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Probably the simplest way to kill your solution is to make system function calls in a loop :)

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

    It is assumed that the no such call is made intentionally. Nonetheless, the // do something loop block in the sample program should do exit(0), break; or return 0; before the elapsed time reaches T if the solution is found and no further iteration is needed.

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

      I meant _clock::now() (it is resolved into a system function call and you call it regularly in the loop).

      • »
        »
        »
        »
        4 years ago, # ^ |
        Rev. 3   Vote: I like it 0 Vote: I do not like it

        I agree with you. The execution time for each std::chrono::high_resolution_clock::now() function call inside the time_elapsed() function should be much smallar than the lower-bound on the execution time of a single iteration of the // do something block.