BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"MilliSecs - The Solution"
, by thechange
Explains the problem and solution to effectively using the MilliSecs () command.
Code
;------------------------------------------------------------------------------- ; ;,,,, ; MilliSecs - Where does it end? ;---- ; ; Some believe that MilliSecs () returns ; the number of milliseconds elapsed since midnight. ; Unfortunately, this is not the case. ; ; The MilliSecs () function returns the number of milliseconds ; elapsed since Windows was first started. ; So right after windows starts, MilliSecs () equals zero. ; ; Since the maximum value of an integer in Blitz is 2,147,483,647 ; MilliSecs () will be counting up to that value. ; How long will that take? ; ; 2147483.7 seconds ; 35791.4 minutes ; 596.5 hours ; 24.9 days ; ; Although most people don't have their computer on that long. ; Besides, it's pretty hard to keep Windows stable ; for such a long time anyway :P ; ; So what happens after that time? ; Take a regular integer variable for example. Local Integer% ; And you assign the maximum value to it. Integer = 2147483647 ; Now add 1 and see what happens. Integer = Integer + 1 Print Integer ; It wraps to -2,147,483,648! ; Same thing happens with the milliseconds. ; ; What is the problem about all this? ; Imagine you have the following code: Const TimeToWait% = 1000 Local Start% Local Finish% Start = MilliSecs () Finish = Start + TimeToWait Repeat Until MilliSecs () >= Finish ; If the number of milliseconds wrap ; in the middle of the Repeat..Until, ; the program could hang forever! ; ; MilliSecs Start Finish ; | | | ; v v v ; ,___,___,___,_ _/\ _ _,___,___,___,___,_ _/\ _ _,___,___,___, ; | | | | | | | \/ | | | | | | | | | \/ | | | | | | | ; | | | ; ; -2,147,483,648 0 2,147,483,647 ; ; If Finish lies right before the wrapping point ; and our program didn't have enough time ; to check MilliSecs before it wraps, ; the resulting value will be even smaller than before, ; and remaining smaller than Finish for another 50 days! ; ; So what's the solution you ask? ; The solution is simply changing orientation to duration. ; Say what you say? ; ; I mean, by comparing duration instead of points in time ; you can still accurately measure time ; without running into any gaps in time. ; ; MilliSecs Start Finish ; | | | ; v v v ; ,___,___,___,_ _/\ _ _,___,___,___,___,_ _/\ _ _,___,___,___, ; | | | | | | | \/ | | | | | | | | | \/ | | | | | | | ; | | : | ; : : ; |<--------->| ; Duration Start = MilliSecs () Repeat ; How much time has elapsed since Start. Elapsed = MilliSecs () - Start ; Quit if enough time has elapsed. Until Elapsed >= TimeToWait ; If you look at the graph above ; and then at the following equation: ; ; MilliSecs () - Start ; ; Then you might wonder why it works. ; ; Let's try it out in a program. MilliSecs = -2147483600 ; 48 after wrapping point. Start = 2147483600 ; 47 before wrapping point. ; Calculate the difference. Elapsed = MilliSecs - Start Print Elapsed ; Which should give you 96. ; Here's what happens: ; ; MilliSecs Start Finish ; | | | ; v v v ; ,___,___,___,_ _/\ _ _,___,___,___,___,_ _/\ _ _,___,___,___, ; | | | | | | | \/ | | | | | | | | | \/ | | | | | | | ; | : | : | ; : : ; ..->| |<---------.. ; Elapsed ; ; And that's all there is to it =) ; ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations