CSC 495: Problem Solving and Programming

Timing Code Examples

This page gives examples of timing code in both Java and C++. There are several approaches to making timing work, but you should keep in mind the following: first, because of the inefficiency of system calls to read the current time, you shouldn't get the time very often – preferably no more than once every several seconds.

The idea with these examples is the following: since we don't know how much time a single iteration of refinement takes, we do a certain number of iterations, and keep doubling the number of iterations until we exceed 10 seconds. Now we do additional iterations in blocks of this many, so each block takes 10 to 20 seconds. Finally, we stop as soon as we exceed the defined "runLimit" - by setting this to 29 minutes, we should never run in longer than 29 minutes and 20 seconds. This has enough safety to make sure it finishes in under 30 minutes, but still runs for about as long as it can.

Java Code

  final double runLimit = 29*60.0;

  double startTime = System.currentTimeMillis()/1000.0;

  initialize();

  long iterations = 0;
  long checkTime = 10, timeStep = 10;
  while (true) {
      refine();
      if (++iterations >= checkTime) {
          double elapsed = System.currentTimeMillis()/1000.0 - startTime;

          if (elapsed >= runLimit)
              break;

          if (elapsed < 10.0) {
              checkTime *= 2;
              timeStep = checkTime;
          } else {
              checkTime += timeStep;
          }
          System.out.println(iterations);
      }
  }

C++ Code

    const double runLimit = 29*60.0;

    struct timeval start;
    gettimeofday(&start, NULL);
    double startTime = start.tv_sec + start.tv_usec/1000000.0;

    initialize();

    long long iterations = 0;
    long long checkTime = 10, timeStep = 10;
    while (true) {
        refine();
        if (++iterations >= checkTime) {
            struct timeval now;
            gettimeofday(&now, NULL);
            double elapsedTime = now.tv_sec + now.tv_usec/1000000.0 - startTime;

            if (elapsedTime >= runLimit)
                break;

            if (elapsedTime < 10.0) {
                checkTime *= 2;
                timeStep = checkTime;
            } else {
                checkTime += timeStep;
            }
        }
    }