It is currently Sat Apr 29, 2017 10:32 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 1, 2  Next
Author Message
 Post subject: Var$ = constant$ against var$ = " ...."
Unread postPosted: Sun Mar 05, 2017 8:42 am 
Offline

Joined: Sat Feb 11, 2017 1:40 am
Posts: 68
Location: France
In almost all languages I used,
Code:

a$ = "x" : b$ = "x" : c$ = "x"

is more time/resources consuming than
Code:
X$ = "x"
a$ = X$  : b$ = X$ : c$ = X$


Is it also true in BASIC ?

_________________
Alain


Last edited by Sprougnaf on Sun Mar 05, 2017 9:35 am, edited 1 time in total.

Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constan$t against var$ = " ...."
Unread postPosted: Sun Mar 05, 2017 9:11 am 
Offline
User avatar

Joined: Mon Aug 18, 2014 1:16 pm
Posts: 1317
Sprougnaf wrote:
In almost all languages I used,
Code:

a$ = "x" : b$ = "x" : c$ = "x"

is more time/resources consuming than
Code:
X$ = "x"
a$ = X$  : b$ = X$ : c$ = X$


Is it also true in BASIC ?

Yes you can do that


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constan$t against var$ = " ...."
Unread postPosted: Sun Mar 05, 2017 9:18 am 
Offline

Joined: Sat Feb 11, 2017 1:40 am
Posts: 68
Location: France
"You can do that" ? The question is to know what is the better, according with how works the interpreter.

_________________
Alain


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Sun Mar 05, 2017 10:26 am 
Offline
User avatar

Joined: Sat Dec 08, 2012 2:14 am
Posts: 1128
Location: From Blighty
I'm not sure of the question.

Firstly, the interpreter via a preprocessor on first parse removes all spacing and converts things to lower case.

Secondly, a colon is treated just as you have written on another line.

Use a timer in your code is the tertiary answer. Test scenarios are always welcome here to provide as examples in good test code. You never know what you might reveal.

Mog


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

Joined: Sat Feb 11, 2017 1:40 am
Posts: 68
Location: France
I do not worry about
columns, but about the management of string litterals.

My hope when posting was to receive a documented answer from somebody well knowing the internal algoritms of BASIC, before my first program is big enough to deserve such improvement and I have to test.

_________________
Alain


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Sun Mar 05, 2017 1:38 pm 
Offline

Joined: Sat May 14, 2011 7:43 am
Posts: 355
Location: South Africa, UK & US (Florida)
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


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Sun Mar 05, 2017 2:03 pm 
Offline

Joined: Sat Feb 11, 2017 1:40 am
Posts: 68
Location: France
Thanks Peter. Not surprising by itself, but I never guessed so great a difference. Then it becomes useless to think of improved memory usage.
I see that working with an interpreter needs to change my old coding habits.

_________________
Alain


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

Joined: Thu Nov 07, 2013 12:51 pm
Posts: 128
I assume that using LET for all variable assignments would speed this up. I may test that when I get chance to see if there is a notable improvement.

_________________
Check out my RFO BASIC! games:
https://play.google.com/store/apps/details?id=com.rfo.NumberDown
https://play.google.com/store/apps/details?id=com.rfo.FallingFreddy
https://play.google.com/store/apps/details?id=com.rfo.WhatsMySong


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

Joined: Sat May 14, 2011 7:43 am
Posts: 355
Location: South Africa, UK & US (Florida)
Yes! That makes a BIG difference (see code below)!
But the variable assignment time still usually (not always, why?) exceeds the literal assignments.
I shall start bothering to use 'Let' more often from now on.
Code:

REM Start of BASIC! Program
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
REM Start of BASIC! Program
T=time() 
For q=1 to 500
Let A$="z"
Let B$="z"
Let C$="z"
Next
? Time()-t
T=time() 
Let X$="z"
For q=1 to 500
Let A$=x$
Let B$=x$
Let C$=x$
Next
? Time()-t

Peter


Report this post
Top
 Profile  
 
 Post subject: Re: Var$ = constant$ against var$ = " ...."
Unread postPosted: Mon Mar 06, 2017 8:28 am 
Offline

Joined: Sat Feb 11, 2017 1:40 am
Posts: 68
Location: France
So, it clearly appears that using LET makes faster the parsing. Here I myself ask 'why?'.

Concerning about what you, Peter, say 'why': I guessed that related with various implementations in memory of variables and litterals, with various executions of the test.
From my Assembly days, I remember that far addresses need more long low-level instructions, hence more time.

_________________
Alain


Last edited by Sprougnaf on Sat Mar 11, 2017 1:23 am, edited 1 time in total.

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


Who is online

Users browsing this forum: No registered users and 11 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