Code for estimating runtimes during job execution

The code below provides a running estimate of time being used per time step (based on last 10 steps) and two estimates of time until the job completes,
based on number of time steps run so far, the number yet to complete, and either • timing of last 10 steps or • elapsed time since the job started.

This produces output looking like this:

Step 1500: time/step  0.09 sec, est. time remaining   1:21 /   1:22
 1500   750.  ... my stats ...
 1600   800.  ... my stats ...
 1700   850.  ... my stats ...
 1800   900.  ... my stats ...
 1900   950.  ... my stats ...
Step 2000: time/step  0.09 sec, est. time remaining   0:36 /   0:36
 2000  1000.  ... my stats ...
 2100  1050.  ... my stats ...
 2200  1100.  ... my stats ...
 2300  1150.  ... my stats ...
 2400  1200.  ... my stats ...

Elapsed time :   219.61 sec (  3:39)
Zones per sec:  8606037


FORTRAN

>>>> Declarations:

!
! ... RUNTIME TIMING variables
!
        real tstart,time1,time2,tlast(0:9),elapsed,estimate1,estimate2, ZPS
        integer min1,sec1,min2,sec2

>>>> Put before main time step loop

! ... Initialize TIMING variables

        tstart = secnds(0.0)

>>>> Put inside at top, or bottom, of main time step loop

! TIMING; report wallclock per-time-step based on
!         last 10 steps, and estimates to complete run.
! first estimate is based on last 10 steps,
! second on time since start of run.  The variable
! "n" is my main time-step-loop counter.

!
          time2 = secnds(0.0)
          if (n .gt. 10) then
            time1     = tlast(mod(n,10))
            elapsed   = time2-time1
            estimate1 = real(nstep-n)* ( (time2-time1)/10.0 )
            min1 = ifix( estimate1 / 60.0 )
            sec1 = ifix( estimate1 - 60.0*real(min1) )
            estimate2 = real(nstep-n) * ( (time2-tstart) / real(n) )
            min2 = ifix( estimate2 / 60.0)
            sec2 = ifix( estimate2 - 60.0*real(min2) )
            write(6,'(''Step '',i4,'': time/step '',f5.2,  &
                       '' sec, est. time remaining '',       &
                       i3,'':'',i2.2,'' / '',i3,'':'',i2.2)')
                       n,(elapsed/10.0),min1,sec1,min2,sec2
          endif
          tlast(mod(n,10)) = time2

>>>> Put after end of main time step loop; I like to use zones/second as a performance benchmark

!
! ... TIMING stats for this run
!
        time2   = secnds(0.0)
        elapsed = time2-tstart
        min2    = ifix( elapsed / 60.0)
        sec2    = ifix( elapsed - 60.0*real(min2) )
        ZPS     = real(nstep)/elapsed*real(nx*ny*nz)
        write(6,'(/,
               ''Elapsed time : '',f8.2,'' sec ('',i3,'':'',i2.2,'')'',/,   &
               ''Zones per sec: '',i8,/)')
               elapsed,min2,sec2,nint(ZPS)

C programs


>>>> Declarations


#include <time.h>      (earlier in code e.g. at top of program)

/* Variables for runtime timing */

        time_t tstart,time1,time2,tlast[10],elapsed;
        float estimate1,estimate2;
        int min1,sec1,min2,sec2;

>>>> Put before main time step loop start

        tstart = time(0);

>>>> Put inside at top (or bottom) inside main time step loop

/*
 * TIMING; report wallclock per-time-step based on
 *              last 10 steps, and estimates to complete run.
 * first estimate is based on last 10 steps,
 * second on time since start of run
 */
          time2 = time(0);
          if (n>10) {
            time1     = tlast[n%10];
            elapsed   = time2-time1;
            estimate1 = (float)(nstep-n) * ( (float)(time2-time1)/10.0 );
            min1 = (int) (estimate1 / 60.0);
            sec1 = (int) (estimate1 - 60.0*(float)min1 );
            estimate2 = (float)(nstep-n)  * ( (float)(time2-tstart) / (float)n );
            min2 = (int) (estimate2 / 60.0);
            sec2 = (int) (estimate2 - 60.0*(float)min2 );
            printf("Step %4d: time/step %5.2f sec, est. time remaining %03d:%02d / %03d:%02d\n",
                  n,(float)elapsed/10.0,min1,sec1,min2,sec2);
          }
          tlast[n%10] = time2;