Stopwatch Library Measure Execution Time


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.


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

This procedure will start the timer. No arguments needed.

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')" );
6   sp_StartTime := CurrentToString( "%c%y-%m-%d %H:%M:%S:%t" );
Function fnc_elapsed

This procedure will stop the timer and return the total time in seconds.

 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);
 9   p_elapsedTime := CurrentToMoment( [tick],  sp_StartTime );
11fnc_elapsed := p_elapsedTime;

Example of Usage

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();
6! Reporting of that time, whereever.
7sp_runTime := formatString("Execution of procedure took %n seconds", p_elapsedTime );

Minimal Requirements

AIMMS Community license is sufficient for working with this example.

Release Notes

  • 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