It is currently Sat Apr 29, 2017 10:29 am



Welcome
Welcome to rfobasic

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today. **You are not required to provide truthful information to any registration questions. Be whomever you wish to be.!


Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Mon Mar 06, 2017 9:08 am 
Offline
User avatar

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

Read jMarc's answer at post12231.html

_________________
- Creator of the Android BASIC! Compiler


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Fri Mar 10, 2017 11:36 pm 
Offline

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 805
[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.
Comments?

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


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Sat Mar 11, 2017 4:25 am 
Offline

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 805
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



Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Sat Mar 11, 2017 5:16 am 
Offline

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

Read jMarc's answer at post12231.html




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


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Sat Mar 11, 2017 2:17 pm 
Offline
User avatar

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.

[edit] 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


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Thu Mar 16, 2017 3:31 am 
Offline

Joined: Wed Oct 03, 2012 9:53 am
Posts: 2795
Location: Colorado, U.S.
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.

A couple more comments:
- 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


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Thu Mar 16, 2017 4:21 am 
Offline

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


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Wed Apr 12, 2017 4:57 pm 
Online
User avatar

Joined: Sat Mar 26, 2016 9:23 am
Posts: 72
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


Report this post
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2


Who is online

Users browsing this forum: No registered users and 5 guests


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

Search for:
Jump to:  
suspicion-preferred