Timer Function

See Also2EOFTY              Example2ET90N8>Low

Returns the number of seconds that have elapsed since 12:00 A.M. (midnight).




The Timer function is automatically used with the Randomize statement to generate a seed3F5TPW7 for the Rnd (random-number) function.  You can also use it to time programs or parts of programs.

See Also

Randomize StatementOEZTQI

Timer Function Example

The example uses the Timer function to keep track of elapsed time required to find all prime numbers between 1 and 1000.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   Const UNMARK = 0, MARKIT = Not UNMARK, MAXNUM = 1000

   Static Mark(MAXNUM)                   ' Set up an array.

   Dim Delta, Finish, I, Msg, N, Num, Start    ' Declare variables.

   Start = Timer                         ' Set start time.

   Num = 0                               ' Initialize number counter.

   For N = 3 To MAXNUM Step 2            ' Find all primes.

      If Not Mark(N) Then

         Delta = 2 * N

         For I = 3 * N To MAXNUM Step Delta

            Mark(I) = MARKIT

         Next I

         Num = Num + 1

      End If

   Next N

   Finish = Timer                        ' Set end time.

   Msg = "The program took " & (Finish - Start)

   Msg = Msg & " seconds to find "

   Msg = Msg & Num & " prime numbers."

   MsgBox Msg                            ' Display message.

End Sub