Fast variables?

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Fast variables?

Post by Shaun_B »

I'm pretty certain that declaring (and therefore initialising) your variables before they are used (in BASIC v2 anyway) has a slightly positive effect on the speed later on in the program. Let's say that I'm going to use A, X, Y, Z and I as integers, C as a floating point number and a$ for strings, one might declare them at line zero:

Code: Select all

0 a=0: x=0: y=0: z=0: i=0: c={pi}: a$=""
That would be the way that most people would do this, but I prefer:

Code: Select all

0 a=0: x=a: y=a: z=a: i=a: c={pi}: a$=""
but I'm actually wondering if this technique (I use something similar in PHP) would be of any use?

Code: Select all

0 a= x= y= z= i= a$= " ": c={pi}
I know it's probably only saved a few bytes (that's if it works as I'm expecting it to: initialising A, X, Y, Z and I to zero and telling BASIC that A$ is going to be used), but like with Javascript, minimisation and obfuscation is always good, with the exception of not being human-readable.

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
merman
Member
Member
Posts: 183
Joined: Mon Apr 14, 2014 2:50 pm
Location: Skegness
Contact:

Re: Fast variables?

Post by merman »

Declaring the variables you use the most first would give a slight increase in speed too.

The third version there might not work - don't think you can do multiple variable assignments like that in Basic 2.0. And it makes things less readable (which I guess is only a consideration if you need to share the code).
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Fast variables?

Post by Shaun_B »

I wasn't trying mass variable assignment in the last example, I was just simply trying to say "Hey BASIC, you've used these variables before" or something. I mean you don't have to declare and initialise any variables. Switch the Commodore 64 on and PRINT A and it will output zero.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Fast variables?

Post by Shaun_B »

Over 33 minutes per test case... I do this so you don't have to (and I really should have used the SuperCPU for this one for sure!) - I'll add the listings afterwards so people can point out the flaws in my testing strategy.

Edit: The time difference is negligible between the 1st, 2nd and 3rd method, so I tried WAIT 53265,128,128:RUN before running this listing (replacing line zero as appropriate and also testing with no variable declarations first):

Code: Select all

0 a=0: i=a: x=a: y=a: z=a: a$=" ": c={pi}: ti$="000000"
1 for a=0 to 9:for i=0 to 9:for x=0 to 9
:for y=0 to 9:for z=0 to 9
2 c=c+0.1:a$=a$+" ":if len(a$)>254 then
a$=""
3 next z, y, x, i, a:print ti, ti$
What's happening to the variables is quite trivial really and not important to the test, so I think, in terms of memory efficiency, method three is better and as I pointed out above before the response from Andrew Merman, the 1st or 2nd methods are simply more readable. When scaled up to the SuperCPU, there is no time difference whatsoever. Also note that I can say that the 3rd method is essentially equivalent to the other two as I'm initialising the integers to zero anyway.

Conclusion: Other than perhaps saving a few bytes, this is quite pointless. However, if you're going to write BASIC programs (as I am doing in this 50th year of the language), declare your variables before you use them, no matter how you do it :-)

Conclusion 2: Don't edit forum posts on a Saturday night after alcohol has been consumed.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
merman
Member
Member
Posts: 183
Joined: Mon Apr 14, 2014 2:50 pm
Location: Skegness
Contact:

Re: Fast variables?

Post by merman »

Shaun_B wrote:I wasn't trying mass variable assignment in the last example, I was just simply trying to say "Hey BASIC, you've used these variables before" or something. I mean you don't have to declare and initialise any variables. Switch the Commodore 64 on and PRINT A and it will output zero.
But it won't work because a null value is not the same as a blank string variable...
tiny tim
Member
Member
Posts: 4
Joined: Tue Jun 24, 2014 8:52 pm
Location: PAL
Contact:

Re: Fast variables?

Post by tiny tim »

Shaun_B wrote:I'm pretty certain that declaring (and therefore initialising) your variables before they are used (in BASIC v2 anyway) has a slightly positive effect on the speed later on in the program.
That's what the DIM-instruction is for:

Code: Select all

dim a,b,c%,d$,...
Speedwise it basically boils down to declaring variables that are used a lot in time-consuming parts of your code (e.g. the innermost of nested for-next-loops) before other variables that are used less frequently.

Furthermore, if your program contains large arrays you should declare all scalars before DIMing the arrays, because every time you add one later the interpreter has to move the whole array-space 7 bytes up in memory to create room for it.

Also, assigning constants to variables may save some time compared to putting the constants into the code in Petscii, because the parsers needs quite a lot of time to convert Petscii to floating point. On the other hand, the more variables you use the longer it takes the interpreter to walk through the variables-list, so there is a tradeoff.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Fast variables?

Post by Shaun_B »

I was not aware that DIM could be used in such a way, thanks for the heads-up.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 6 guests