# A simple MSX Basic question

Page 1/3
| 2 | 3

hello,

I would like to know what expression is faster in MSX-BASIC

`IF BA>0 THEN XB=XB+IB`

or

`XB=XB-IB*(BA>0)`

Obviously, BA is a flag to know if bullet is fired and XB,YB its coordinates. So, my real question is to know whether it is worth to use the second expression bearing BA is not always >0 in mind.

Thank you

IF is faster, specially if the condition is not met.
Check this quick example: https://msxpen.com/codes/-LQOD2EH4x_R66vgbbTP

Thanks I don't really know how MSX Basic works inside. I though that the second expression would spend less memory making code faster at the end.

by the way, good idea to use MSXPen.

I did some research when coding Pérez the Mouse for Konamito's MSX-BASIC contest.
Particularly, I found something really particular about expressions: A=B*5+10 is faster that A=10+5*B. My guess is that in the first expression MSX-BASIC can evaluate things as they come, but in the second one it has to "stack" the 10 to compute the other operand, then "stack" the 5 to look for the value of the variable, and then evaluates the expression.
Most of the optimization tricks were explained (in Spanish only) in the readme file distributed with the game. Maybe you can have a look at it to get some optimization ideas!

You can time it yourself, right?

```10 DEFINT A-Z
20 XB = 0 : IB = 2 : BA = 1
30 TIME = 0
40 FOR I = 0 TO 1000
50 XB = XB - IB * (BA > 0)
60 NEXT
60 PRINT TIME
```

Results for the turboR (R800 turbo mode):

• For the IF I get 33 if BA = 0, and 53 if BA = 1.
• For the multiplication I get 56 if BA = 0, and 59 if BA = 1.

Depending on the video refresh frequency, one TIME tick is either 1/50th or 1/60th of a second. In this case, it’s 1/60th.

I don't know whether Konamito will launch the contest this year or not, but I would like to be ready. I'm testing some codes to make something harder than other years. That is the reason of my question.

theNestruo wrote:

Most of the optimization tricks were explained (in Spanish only) in the readme file distributed with the game. Maybe you can have a look at it to get some optimization ideas!

Yes, I will do it.

I remember that there are some tricks as calling variable at the beginning of the code for hastening it, but I am not able to remind where I have read it. Actually I'm looking for little tricks to make MSX Basic faster. One of them that I have found is to change this piece of code

`IF A=7 AND B=8 THEN ...`

for this one

`IF A=7 THEN IF B=8 THEN ...`

Noting that A is the variable less feasible to be changed

It is multiplication (and division) that takes time in the example above.

And the difference between

IF A=7 AND B=8 THEN ...

and

IF A=7 THEN IF B=8 THEN ...

depends on the frequency A=7 or B=8 to be true. If A=7 most of the time, I think the second method will be slower. You have to put the least often true condition at first in this cas.

That is what meant theNestruo wrote:

Most of the optimization tricks were explained (in Spanish only) in the readme file distributed with the game. Maybe you can have a look at it to get some optimization ideas!

Sorry, I am not able to get the readme file you said. The only web I could download the game was this https://msxgamesworld.com/gamecard.php?id=3178 where the available .zip file hasn't any readme. Maybe it would be a good idea to upload in this threat if you don't mind.

Edit: Another idea to get faster is not use arrays :)
https://msxpen.com/?code=-LQTII3Ya4oH1HJ8Xd_B

Is the zip file you are downloading named "Perez the Mouse (theNestruo, 2011).zip" or "Perez the Mouse (theNestruo, 2011) (v1.0).zip"?
The first one is under the "View more options" big button near the top of the page > "download now". This is the zip file that contains "source.sp.html".
The second zip file (the one that ends with "(v1.0)" is scrolling down, under the "miscellaneous" section, and does not contain source.sp.html".

Anyway, I've uploaded the file to my GitHubGist (source.sp.html)) so it can be downloaded more easily :)

Page 1/3
| 2 | 3