/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ < > < The Liberty Basic Newsletter > < Issue #81 - 1st SEP 2000 > < © 2000, Cameron Arnott > < All Rights Reserved > < > \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ This newsletter was Written using >=- Windows Notepad >=- Font: Fixedsys regular 9 Wordwrap: on Width: 70 Characters LB2.0 Alpha7 >=- Font: Fixedsys regular 9 LB 1.43 Alpha >=- Font: Fixedsys 8@15# LB 1.42 >=- Font: Fixedsys 8@15# Outlook Express >=- Sent: as Text Font: Fixedsys regular 9 Wordwrap: 76 Characters ----------------------------------------------------------------------- In this issue >=- Liberty Basic Version checking Timer Program Optomized LB2 timer$("milliseconds") How to use Timer.dll (dll by Stephen@KiwiSoft) How to use timeGetTime (part of mmsystem.dll) Benchmark Timer More on Real Time Loops Attachments >=- nl0081.zip nl0081.txt This newsletter version.bas LB Version Test timer.bas Test Timer rttimer.bas Timer Program rttimer2.bas timer.dll Timer Program mmtimer.bas mmsystem.dll Timer Prog rtbt.bas Real Time Bench Test Required for Samples>=- call32.dll Available from http://www.egroups.com/files/lbnews/LBnewsTopics/call32.dll timer.dll Available from Stephen Email:http://www.egroups.com/files/lbnews/timer.zip In future Issues >=- Programming Etiquette How do I start writing a program Spotlights on fellow LB programmers Your Submissions.. YES YOURS To submit Articles for future editions >=- send email to mailto:blue_steel@globalfreeway.com.au
Carl Gundel: Author of Liberty Basic has anounced that LB2.0 alpha8 should be released later this month.. (Well thats the plan)
Welcome to Issue #81 of the Liberty Basic newsletter.
Hope you like the new format.
Liberty Basic Version
First of all lets find out what version of LB we are using.
How? There is a special variable in Liberty Basic that will tell you what version you are running. What we do is write a test to read that variable.
'--------------- Check Version [Check_Version] if val(left$(Version$,3)) < 2.0 then goto [V1.4] [V2.0] print "The version of LB you are using is V2.0 or later" end [V1.4] print "The version of LB you are using is V1.4 or earlier" end '-------------- End of Check Version
If you don't check the version that you are running, it could crash LB if you are using newer features/instructions which were not there in earlier versions. It is always good practice to check the version you are running at the start of any program. It's not good enough just to tell someone somewhere in the docs what version is required, as they might not read them. Then they'll complain to you that your program doesn't work.
Timer program
To Time our tests we'll use the "timeGetTime" routine in the mmsystem.dll which is a part of Windows.
'--------------- Timer Program [TimerOn] open "mmsystem.dll" for dll as #mm calldll #mm, "timeGetTime",_ startTimer as long [test] ' ---- Sample test ---- For loop = 1 to 180 print "#"; Next loop [TimerOff] calldll #mm, "timeGetTime",_ endTimer as long Close #mm print print "The time elapsed was ";endTimer-startTimer;" Milliseconds" '--------------- End of Timer Program
More on Real Time loops
As I said in the last newsletter, real-time loops are important. There are lots of ways to achieve them. I showed you one, now I'll show you an optimised version of it, as well as two other methods.
The three methods we will be comparing are >=-
'timer$("milliseconds")' LB2.0 instruction
timer.dll by Stephen @KiwiSoft
mmsystem.dll Windows library
'------ Optimised LB2.0 Timer$("milliseconds")
[Check_Version]
if val(left$(Version$,3)) < 2.0 then goto [V1.4]
[V2.0] ' Change these variables to change test timings times = 5 'how many times to loop loopTime = 100 'how long you want it to take for each loop for i = 1 to times ' Real Time Timing Routine timeNow = time$("milliseconds") result=0 while loopTime > result result = time$("milliseconds") - timeNow wend 'End of Real Time Timing Routine c = c + 1 print c next i end [V1.4] print "The program requires at least Liberty Basic v2.0" end '------ End of Optimised LB2.0 Timer$("milliseconds") '------ Timer.dll (dll by Stephen @KiwiSoft) ' Change these variables to change test timings times = 5 'how many times to loop loopTime = 100 'how long you want it to take for each loop ' Thunking Routing open "call32.dll" for dll as #call32 calldll #call32, "Declare32", _ "_DELAY" as ptr, _ "timer.dll" as ptr, _ "i" as ptr, _ idtimer as word ' end of Thunking for loop = 1 to times ' Real Time Timer calldll #call32, "Call32", _ loopTime as long, _ idtimer as long, _ result as long ' end of Real Time Timer c = c + 1 print c next loop close #call32 '------ End of Timer.dll (dll by Stephen @KiwiSoft) '------ mmsystem.dll (dll included in Windows) ' Change these variables to change test timings times = 5 'how many times to loop loopTime = 100 'how long you want it to take for each loop open "mmsystem.dll" for dll as #mm for i = 1 to times ' Real Time Timer Routing calldll #mm, "timeGetTime",_ result as long newTime = result while loopTime > newTime-result calldll #mm, "timeGetTime",_ newTime as long wend ' end of Real Time Timer Routine c = c + 1 print c next i close #mm '------ End of mmsystem.dll (dll included in Windows)
BenchMarking Real Time Programming
We now have two or three ways to handle real time looping. Lets compare them. How? Well, we've learned how to do everything we need in this newsletter. :-) So lets put them all together to create ourselves a simple Benchmark program
'------ BenchMark RealTime Loops program ' ' Change these variables to change test timings times = 5 'how many times to loop loopTime = 100 'how long you want it to take for each loop [Check_Version] if val(left$(Version$,3)) < 2.0 then goto [V1.4] [V2.0] ' Using LB2's time$("milliseconds") open "mmsystem.dll" for dll as #mm calldll #mm, "timeGetTime",_ start as long for i = 1 to times timeNow = time$("milliseconds") result=0 while loopTime > result result = time$("milliseconds") - timeNow wend c = c + 1 print c total = total + result next i calldll #mm, "timeGetTime",_ end1 as long close #mm print "Results of test using LB2's time$("+chr$(34)+_ "milliseconds"+chr$(34)+")" print "The average milliseconds the dll was delaying for "+_ "was: " + str$(int(total/times)) print "The average time it took to complete the hole loop was" print str$(int((end1-start)/times)) + " and should " +_ "have taken " + str$(loopTime) [V1.4] ' Sample from Steve on how to use his great new timer.dll ' only Variable names have been altered to change global test ' variable values easily at the top of the program c = 0 ' resrt the c variable total = 0 ' reset the total variable to 0 open "call32.dll" for dll as #call32 'open dll calldll #call32, "Declare32", _ 'Define call "_DELAY" as ptr, _ "timer.dll" as ptr, _ "i" as ptr, _ idtimer as word open "mmsystem.dll" for dll as #mm calldll #mm, "timeGetTime",_ start as long for i = 1 to times calldll #call32, "Call32", _ loopTime as long, _'specifying loop length idtimer as long, _ result as long 'Returns the delay c = c + 1 print c total = total + result next i calldll #mm, "timeGetTime",_ end1 as long close #mm close #call32 print "Results of test using Steven's timer.dll" print "The average milliseconds the dll was delaying for "+_ "was: " + str$(int(total/times)) print "The average time it took to complete the hole loop was" print str$(int((end1-start)/times)) + " and should " +_ "have taken " + str$(loopTime) ' using Windows mmsystem.dll c = 0 ' reset the c variable total = 0 ' reset the total variable to 0 open "mmsystem.dll" for dll as #mm calldll #mm, "timeGetTime",_ start as long for i = 1 to times calldll #mm, "timeGetTime",_ result as long newTime = result 'initialize newTime value while loopTime > newTime-result calldll #mm, "timeGetTime",_ newTime as long wend c = c + 1 print c total = newTime+total-result next i calldll #mm, "timeGetTime",_ end1 as long close #mm print "Results of test using mmsystem.dll" print "The average milliseconds the dll was delaying for "+_ "was: " + str$(int(total/times)) print "The average time it took to complete the whole loop was" print str$(int((end1-start)/times)) + " and should " +_ "have taken " + str$(loopTime) '------ End of program See if you can find any mistakes in the program. :-) Can you see all that I covered earlier in this newsletter? Try running the benchmark program. I'd be interested in seeing the results. Try altering the values set for :=- ' Change these variables to change test timings: times = 5 'how many times to loop loopTime = 100 'how long you want it to take for each loop at the top of the program. Notice how they differ. Its up to you which you decide to use.. Just remember: have fun programming :-) Till next time, Cameron Arnott Here are the results from my system :-) 1 2 3 4 5 Results of test using LB2's time$("milliseconds"): The average milliseconds the dll was delaying for was: 110. The average time it took to complete the whole loop was 103 and should have taken 100. 1 2 3 4 5 Results of test using LB2's time$("milliseconds"): The average milliseconds the dll was delaying for was: 110. The average time it took to complete the whole loop was 103 and should have taken 100. 1 2 3 4 5 Results of test using mmsystem.dll: The average milliseconds the dll was delaying for was: 100. The average time it took to complete the whole loop was 105 and should have taken 100.