BBS: Inland Empire Archive Date: 05-05-92 (10:36) Number: 166 From: RICHARD VANNOY Refer#: NONE To: ZACK JONES Recvd: NO Subj: Forgotten Statement Conf: (2) Quik_Bas
ZJ>Not that I doubt you, but how do you guys (read readers of this echo)
ZJ>know that one function/command is faster than the others? Like every
ZJ>other programmer I want to learn how to write the smallest, fastest code
ZJ>I can - I'd just like to know how you guys come up with your answers
ZJ>(Years of experience perhaps?)
Here's an example where a VERY short and fast statement can
be repeated many times and timed. Try it. Once you get
the idea, just replace the two code samples with any two
operations you want to compare.
Which is faster?
PRINT x$; y$; z$ or PRINT x$ + y$ + z$
Let's find out.
'BENCH.BAS - simple benchmark program example
'Note: The code between the two Start! and Done! lines
' must be identical (except for the code being tested)
' to get valid results.
DEFINT A-Z
CLS
X$ = STRING$(25, "X") 'create the test strings
Y$ = STRING$(25, "Y")
Z$ = STRING$(25, "Z")
Synch! = TIMER 'synchronize to the system timer
DO 'This is like putting a horse in a
Start! = TIMER 'starting gate. It holds up the
LOOP WHILE Start! = Synch! 'program until a "new" second
'rolls around.
FOR X = 1 TO 1000 '1000 times is sufficient. I
'usually start with 100 or 1000
LOCATE 1,1
PRINT X$; Y$; Z$ 'Here's sample 1
NEXT
Done! = TIMER 'calculate elapsed time
Test1! = Done! - Start! 'There's the time for the first one
Synch! = TIMER 'Get out the starting gate again.
DO
Start! = TIMER
LOOP WHILE Start! = Synch!
FOR X = 1 TO 1000
LOCATE 1,1
PRINT X$ + Y$ + Z$ 'Here's sample 2
NEXT
Done! = TIMER
Test2! = Done! - Start! 'Sample 2 time is.....
'Now print results
PRINT USING "####.#### seconds using three strings"; Test1!
PRINT USING "####.#### seconds using concatenation"; Test2!
That's all there is to it!
> MegaMail 2.1b #0:"Happiness is a warm puppy." said the anaconda.
--- WM v2.01/91-0012
* Origin: Com-Dat BBS Hillsboro, OR. HST (503) 681-0543 (1:105/314)

Books at Amazon:
Back to BASIC: The History, Corruption, and Future of the Language
Hackers: Heroes of the Computer Revolution (including Tiny BASIC)
Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution
The Advent of the Algorithm: The Idea that Rules the World
Moths in the Machine: The Power and Perils of Programming
Mastering Visual Basic .NET