TIME resultVariable

Reads the system clock and puts the result in resultVariable. The time is expressed in milliseconds from a base time. The base time is midnight, January 1, 1970 UTC. While the unit of time is one millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds. You can use the second example program on the right to estimate the resolution of your system's timer.

You can compute the amount of time in milliseconds between two events in your program by invoking the TIME command at the beginning and the end and computing the difference between the two times returned. For example:

'Estimate the time it takes to sort 10000 numbers
COPY 1000 repeatCount
REPEAT repeatCount
   NORMAL 10000 0 1 vec  'Create 10000 random elements
   TIME start            'Read the start time
   SORT vec sortedVec    'Sort 10000 elements
   TIME end              'Read the end time
   SUBTRACT end start sortTime  'Compute sort duration
   SCORE sortTime sortTimes 'Keep list of durations
PRINT sortTimes
MEAN sortTimes meanSortTime
PRINT meanSortTime

This produced the following results. The time will vary depending on the speed of your CPU.

sortTimes: (9.0 9.0 9.0 9.0 11.0 8.0 10.0 . . .
meanSortTime: 6.33

Here is a program that you can use to estimate the resolution of your system clock's millisecond timer. It reads the clock to get a start time, then reads it over and over until the time changes. Then it saves the difference between the two times. It repeats that process 1000 times, then computes the mean and standard deviation of the time difference.

   TIME startTime
   TIME endTime
   WHILE endTime = startTime
      TIME endTime
   LET resolution = endTime - startTime
   SCORE resolution resolutions
MEAN resolutions ResolutionsMean
STDEV resolutions ResolutionsStdDev
PRINT resolutionsMean resolutionsStdDev