Stopwatch Library

https://img.shields.io/badge/AIMMS_4.87-ZIP:_Stopwatch_Library-blue https://img.shields.io/badge/AIMMS_4.87-Github:_Stopwatch_Library-blue https://img.shields.io/badge/AIMMS_Community-Forum-yellow Measure Execution Time

Introduction

This toolkit library is used to facilitate measuring specific blocks of code by using two procedures, one to start the timer, and one to retrieve the execution time.

See also

In this article deeper explanations about how to measure execution time with and without this library can be found.

Instructions

This chapter is divided into three sections:

  1. Adding the Library

  2. Main Procedures

  3. Example of Usage

Adding the Library

To add and use this library to your project, first download the code and after, follow these steps on how to add an existing library to a project.

Main Procedures

Procedure pr_start(ep_stopwatch)

This procedure will start the timer(ep_stopwatch). Note that ep_stopwatch is optional, and usually not specified.

1! Use the CurrentToString AIMMS function to store the current time
2! in YYYY-MM-DD HH:MM:SS:TT format
3if p_option_Use_UTC_forcaseandstartenddate_active then
4   sp_startTime := CurrentToString( "%c%y-%m-%d %H:%M:%S:%t%TZ('UTC')" );
5else
6   sp_StartTime := CurrentToString( "%c%y-%m-%d %H:%M:%S:%t" );
7endif;
Function fnc_elapsed(ep_stopwatch)

This procedure will return the total time in seconds since the timer ep_stopwatch started. Note that ep_stopwatch is optional, and usually not specified.

 1if p_option_Use_UTC_forcaseandstartenddate_active then
 2   sp_stopMoment := CurrentToString( "%c%y-%m-%d %H:%M:%S:%t%TZ('UTC')" );
 3   p_elapsedTime := StringToMoment(
 4      Format        :  "%c%y-%m-%d %H:%M:%S:%t%TZ('UTC')",
 5      Unit          :  [tick],
 6      ReferenceDate :  sp_startTime,
 7      Timeslot      :  sp_stopMoment);
 8else
 9   p_elapsedTime := CurrentToMoment( [tick],  sp_StartTime );
10endif;
11fnc_elapsed := p_elapsedTime;

There are ten stopwatches available.

Set s_stopwatches {
        Index: i_stopwatch;
        Definition: ElementRange(0,9,prefix:"stopwatch-");
}
Procedure pr_scheduleOver

This is a small frontend to ScheduleAt(), and accepts an elapsed time in seconds, (and a payload procedure):

Procedure pr_scheduleOver {
        Arguments: (p_noSeconds,ep_payLoad);
}
Function fnc_now

The current time, up to seconds precise, timezone UTC is often used to mark moments, and log events.

Function fnc_now {
        Range: string;
}

Basic Example

Below there is an example of usage for measuring pr_longRunningProcedure execution time. Line-7 is simply constructing a message using the stopwatch results.

1! Measuring time of some long running procedure.
2stopwatch::pr_start();
3pr_longRunningProcedure();
4p_elapsedTime := stopwatch::fnc_elapsed();
5
6! Reporting of that time:
7sp_runTime := formatString("Execution of procedure took %n seconds", p_elapsedTime );

Extended Example

The default stopwatch for both pr_start and fnc_elapsed is 'stopwatch-0'. When this stopwatch is “in use”, it is not possible to use this stopwatch for another procedure, say pr_thisSpecificTask. to measure the time spent on pr_thisSpecificTask, you can use another stopwatch; leading to the code:

1! Measuring time of some long running procedure.
2stopwatch::pr_start('stopwatch-1') ;
3pr_thisSpecificTask();
4p_elapsedTimeSpecific := stopwatch::fnc_elapsed('stopwatch-1');
5
6! Reporting of the time spent on a specific task:
7sp_runTime := formatString("Execution of procedure pr_thisSpecificTask took %n seconds",
8        p_elapsedTimeSpecific );

Minimal Requirements

AIMMS Community license is sufficient for working with this example.

Release Notes

v1.3 (10/10/2024)

Added fnc_now, pr_scheduleOver, and the stopwatch optional argument to pr_start, and pr_elapsed.

  • In 2011, Guido Diepen developed the first Stopwatch section and blogged about it.

  • In 2022, Chris Kuip made a small library out of that section, to

    • ease even further the adding of the functionality to an application, and

    • to cater for multi timezone support, see option Use_UTC_forcaseandstartenddate