Q(uick)BASIC Function: TIMER
Quick View
TIMER
A function that returns the number of seconds elapsed since midnight
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- TIMER
Description/Parameter(s)
- Use TIMER to time programs or parts of programs, or with the RANDOMIZE statement to seed the random-number generator.
Example
RANDOMIZE TIMER
Syntax
- TIMER
Description/Parameter(s)
The TIMER function can be used with the RANDOMIZE statement to generate a random number. It can also be used to time programs or parts of programs.
Example
The following program searches for the prime numbers from 3 to 10,000 using a variation of the Sieve of Eratosthenes. The TIMER function is used to time the program.
'*** Programming example for the TIMER function
'
DEFINT A-Z
CONST UNMARK = 0, MARKIT = NOT UNMARK
DIM Mark(10000)
CLS 'Clear the screen
Start! = TIMER
Num = 0
FOR N = 3 TO 10000 STEP 2
IF NOT Mark(N) THEN
'PRINT N, 'To print the primes, remove the
'remark delimiter in front of the
'PRINT statement.
Delta = 2*N
FOR I = 3*N TO 10000 STEP Delta
Mark(I) = MARKIT
NEXT
Num = Num + 1
END IF
NEXT
Finish! = TIMER
PRINT
PRINT "Program took";Finish! - Start!;
PRINT "seconds to find the";Num;"primes"
END
Sample Output:
Program took .1601563 seconds to find the 1228 primesSyntax
- TIMER
Description/Parameter(s)
Usage Notes
- The TIMER function can be used with the RANDOMIZE statement to generate a random number. It also can be used to time programs or parts of programs.
Example
The following program searches for the prime numbers from 3 to 10,000 using a variation of the Sieve of Eratosthenes. The TIMER function is used to time the program.
DEFINT A-Z
CONST UNMARK = 0, MARKIT = NOT UNMARK
DIM Mark(10000)
CLS 'Clear the screen.
Start! = TIMER
Num = 0
FOR N = 3 TO 10000 STEP 2
IF NOT Mark(N) THEN
'PRINT N, 'To print the primes, remove the
'remark delimiter in front of the
'PRINT statement.
Delta = 2 * N
FOR I = 3 * N TO 10000 STEP Delta
Mark(I) = MARKIT
NEXT
Num = Num + 1
END IF
NEXT
Finish! = TIMER
PRINT
PRINT "Program took"; Finish! - Start!;
PRINT "seconds to find the"; Num; "primes"
END
Sample Output:
Program took .28125 seconds to find the 1228 primesSee also: