It is currently Thu Jun 22, 2017 1:41 pm

Welcome
Welcome to rfobasic

 Page 2 of 2 [ 18 posts ] Go to page Previous  1, 2
 Print view | E-mail friend Previous topic | First unread post | Next topic
Author Message
 Post subject: Re: Var\$ = constant\$ against var\$ = " ...."Posted: Mon Mar 06, 2017 9:08 am

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5537
Location: Paris, France
Sprougnaf wrote:
So, it clearly appears that using LET makes faster the parsing. Here I myself ask 'why?'.

_________________
- Creator of the Android BASIC! Compiler

Top

 Post subject: Re: Var\$ = constant\$ against var\$ = " ...."Posted: Fri Mar 10, 2017 11:36 pm

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 834
[email protected] wrote:
I am no such guru, but the following test seems to demonstrate that in BASIC! the converse of your proposition is true, and that individual assignment of literals is about twice as fast as assigning the value of a variable. Not really surprising as the runtime has to search for the variable each time.

Code:

REM Start of BASIC! Program
T=time()
For q=1 to 500
A\$="z"
B\$="z"
C\$="z"
Next
? Time()-t
T=time()
X\$="z"
For q=1 to 500
A\$=x\$
B\$=x\$
C\$=x\$
Next
? Time()-t

Peter

Since there is always much deviation("noise") when doing speed tests, i would recommend to test which much more cycles and also doing some (minimal) statistics.

Code:
n1 = 10
n2 = 5000

DIM ratio [n1]

FOR i=1  TO n1

T= TIME()
FOR q=1 TO n2
A\$= "z"
B\$= "z"
C\$= "z"
NEXT
t1= TIME()-t

T=TIME()
X\$= "z"
FOR q=1 TO n2
A\$=x\$
B\$=x\$
C\$=x\$
NEXT
t2= TIME()-t
?  i, t1,t2, t1/ t2
ratio [i] = t1/ t2

PRINT "--"

NEXT

ARRAY.AVERAGE    ratio, ratio[]
ARRAY.STD_DEV    std,   ratio[]

?
? ratio, std

With this, at my device, the 1st variant is about 20% faster then the 2nd.

But the result is strongly depending on the string-size: with increasing size, 1st variant becomes slower and slower...

Already with a size of three ("zzz"), 1st variant is about 15% slower.

regards, brochi

Top

 Post subject: Re: Var\$ = constant\$ against var\$ = " ...."Posted: Sat Mar 11, 2017 4:25 am

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 834
Related to LET: w/ or w/o is almost the same in latest versions. Once there WAS a big advatage, but it's gone (but in a "good way": w/o became as fast as w/ (..and not v.-v.))

Code:

n1 = 20
n2 = 20000

X\$= "z"

DIM ratio [n1]

FOR i=1  TO n1

T= TIME()
FOR q=1 TO n2
A\$ = x\$
B\$ = x\$
C\$ = x\$
NEXT
t1= TIME()-t

T=TIME()
FOR q=1 TO n2
LET A\$= x\$
LET B\$= x\$
LET C\$= x\$
NEXT
t2= TIME()-t

?  i, t1,t2, t1/ t2
ratio [i] = t1/ t2

PRINT "--"

NEXT

ARRAY.AVERAGE    ratio, ratio[]
ARRAY.STD_DEV    std,   ratio[]

?
? ratio, std

Top

 Post subject: Re: Var\$ = constant\$ against var\$ = " ...."Posted: Sat Mar 11, 2017 5:16 am

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 834
mougino wrote:
Sprougnaf wrote:
So, it clearly appears that using LET makes faster the parsing. Here I myself ask 'why?'.

But be careful: in that example, three things became changed in one step, (...therefore it's not a suitable example to justify LET)

a) "x++" vs. "x = x + ..."
-> 1st significantly faster than 2nd

b) "x=x+one" vs. "x=x+1"
-> 1st significantly faster than 2nd

c) "LET x= ..." vs. "x= ..."
-> almost no effect

Variant a) has by far the strongest influence ...var b) only about 20%

regards, brochi

Top

 Post subject: Re: Var\$ = constant\$ against var\$ = " ...."Posted: Sat Mar 11, 2017 2:17 pm

Joined: Thu Jan 08, 2015 11:28 am
Posts: 1088
Location: .NET
Regarding a\$ = "x" versus a\$ = X\$ this is due to the fact that BASIC doesn't use references as optimization, something many other languages do, which, as said before, ends up in an extra lookup for the latter assignment.

Why LET is faster, because the directly interpreted (no immediate structures) nature of BASIC, it will immediately know that the next operation will be an assignment, removing ambiguity from interpretation.
LET {it knows here it's assignment} a\$ = "x"
a\$ = "x" {--..--}

I may not be 100% correct though.

 oh yeah this is me being sleepy as always and not noticing the second page of posts

_________________
https://github.com/evolbug
http://toobasic.jimdo.com

Top

 Post subject: Re: Var\$ = constant\$ against var\$ = " ...."Posted: Thu Mar 16, 2017 3:31 am

Joined: Wed Oct 03, 2012 9:53 am
Posts: 2802
Excellent answers, all. Sorry I wasn't here to chime in -- getting v01.91 out by Google's deadline took my full allocation of BASIC! cycles. (Strictly speaking, I'm not done with that yet. But I see that we're still available on the Play Store, so I guess I got enough of it right!)

Sprougnaf, the thing implicit in all of these answers is that the Java code that interprets BASIC! stores symbols and variables in lists of Java objects. You're probably used to a symbol table that contains addresses in a block of RAM somewhere. BASIC! doesn't do that. The BASIC! symbol table contains references to other Java objects, or indices into arrays of Java objects. Every variable is one or more objects, and every value is another object. There's a lot of overhead, and it's often very difficult to predict whether one approach will be faster than another.

Another important thing to understand is that there is no tokenizer. BASIC! finds commands by comparing every command group name in a table, one at a time, with the text at the current position in the program string. Java is amazingly fast at doing that, some of things I've tried that would "obviously" make command searches faster actually made them slower. If every one of the 120 or so comparisons fails to match, then the interpreter decides that the command must be LET or CALL! That's why LET makes things faster. You have seen that BASIC! ignores spaces -- in fact the preprocessor squeezes out all unquoted whitespace before the program starts. That's only possible because there is no tokenizer.

But I spent a lot of my BASIC! cycles in 2014 and 2015 trying to find ways to make BASIC! faster. (Unfortunately, I didn't work on BASIC! in 2016 much at all.) One thing I did in 2015 was to cache the command lookup. If you put a command in a loop, the command-table search happens only once. That's why LET doesn't make as much difference as it used to, as brochi says.

But, as evolbug says, the variable look-ups are not cached at all. Java's string-handling is fast, but not infinitely fast, so longer variables take longer to parse, each and every time they're encountered in the program text.

- Timing things on Android is really tricky, because Android doesn't timeslice smoothly. On top of high-priority system things going on all the time, the garbage collector makes huge lumps in the timing. BASIC! has never been optimized to minimize object generation. The garbage collector runs a LOT.
- Saving memory matters only if you're moving around big bitmaps and such. When you start BASIC!, it creates several thousand objects, some of them quite large. I don't remember the number now, but it's at least several megabytes -- and your program hasn't even started yet.

- Marc

Top

 Post subject: Re: Var\$ = constant\$ against var\$ = " ...."Posted: Thu Mar 16, 2017 4:21 am

Joined: Sat Feb 11, 2017 1:40 am
Posts: 68
Location: France
Thanks for cleared many points, Marc. Also Brochi, who gives the best answer to my initial asking.

Considering that running time is mainly used to parse, caching loops is a very good thing. Disappointing to read it cannot manage variables.

_________________
Alain

Top

 Post subject: Re: Var\$ = constant\$ against var\$ = " ...."Posted: Wed Apr 12, 2017 4:57 pm

Joined: Sat Mar 26, 2016 9:23 am
Posts: 94
The keyword "LET" tells the interpreter immediately what's happening - assigning a value to a variable. Or creating a variable.

Otherwise the interpreter has to figure out what' supposed to happen.
It takes time

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 2 of 2 [ 18 posts ] Go to page Previous  1, 2

#### Who is online

Users browsing this forum: No registered users and 3 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Introduce yourself Announcements Questions or comments about BASIC! HTML & JavaScript    HTML & JavaScript Tutorials    Shared HTML & JavaScript programs and examples    HTML & JavaScript Questions and Tips    Bugs and Enhancement Requests concerning the HTML Interface. Shared BASIC! Programs Enhancement Requests Bug Reports Suggestions for improving the manual. Tools for BASIC!    Quick APK       Announcements       Questions and comments    App Builder       Announcements       Questions and comments       Bug Reports    GW Lib    GraphicControls    RFODESIGNER    Android BASIC! Compiler    TooBASIC       Questions and Comments       Bug Reports       Enhancement Requests
suspicion-preferred