LB Booster
« translation of FOR by LBB »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:47am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: translation of FOR by LBB  (Read 94 times)
tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx translation of FOR by LBB
« Thread started on: Dec 10th, 2017, 7:54pm »

Hello Richard
by testing this code on JB forum
Code:
Print "Running 4 timed tests please wait..."
n = 100000
t = time$("ms")
x = 0 : i = 0
while i < n
    i = i + 1
    x = x + i
wend
t2 = time$("ms")
x = 0 : i = 0
[do]
    i = i + 1
    x = x + i
    if i < n then goto [do]
t3 = time$("ms")

x = 0 : i = 0
10
    i = i + 1
    x = x + i
    if i < n then goto 10
t4 = time$("ms")

x = 0
for i = 0 to n
    x = x + i
next
t5 = time$("ms")

print n; " times adding in While... Wend time is ";t2-t
print n; "     times adding in GOTO [do] loop time is ";t3-t2
print n; "     times adding in GOTO 10 loop time is ";t4-t3
print n; "     times adding in FOR loop time is ";t5-t4
 

I naturally run it in LBB as well, to see how it goes.
And in LBB FOR is significantly faster.
I turned on LBB pane - and I saw FOR translated to
Code:
25 FOR i = 0 TO n : WHILE 0 > n EXIT FOR : ENDWHILE
26 x = x + i
27 NEXT 

So I just wonder what
Code:
WHILE 0 > n EXIT FOR : ENDWHILE 

bit is for?

EDIT I wonder if it will run even faster without this check - it looks it is executed on every iteration.
« Last Edit: Dec 10th, 2017, 8:08pm by tsh73 » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: translation of FOR by LBB
« Reply #1 on: Dec 10th, 2017, 9:20pm »

on Dec 10th, 2017, 7:54pm, tsh73 wrote:
So I just wonder what WHILE 0 > n EXIT FOR : ENDWHILE bit is for?

It's to ensure that LBB accurately emulates LB's behavior when the 'initial conditions' of the FOR loop result in it not being executed at all. BBC BASIC always executes the body of the loop at least once irrespective of the initial conditions.

The somewhat strange formulation (using WHILE instead of IF) is necessary so that it works even if the loop is in a single-line IF clause such as this:

Code:
IF condition THEN FOR index = start TO end : PRINT index : NEXT : ELSE .... 

This is unlikely but LBB tries to cover even such eventualities, when possible!

Quote:
I wonder if it will run even faster without this check - it looks it is executed on every iteration.

You can easily do the test. If you specify the initial conditions as constants rather than variables LBB doesn't need to insert that code, because it knows that the special case doesn't apply. So you can compare the speed of these two versions:

Code:
n = 100000
t4 = time$("ms")
x = 0
for i = 0 to n
    x = x + i
next
t5 = time$("ms")
x = 0
for i = 0 to 100000
    x = x + i
next
t6 = time$("ms")

print n; " times adding in FOR loop (variable) time is ";t5-t4
print n; " times adding in FOR loop (constant) time is ";t6-t5 

Richard.
User IP Logged

tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: translation of FOR by LBB
« Reply #2 on: Dec 11th, 2017, 05:06am »

Thank you, Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls