Compare commits

...

2 Commits

Author SHA1 Message Date
tilleul ceeb267746
Update README.md 2022-07-06 11:04:27 +02:00
tilleul 9eb4fb7402
Additional info when using addition instead of multiplying by 2 2022-07-06 11:02:40 +02:00
1 changed files with 41 additions and 6 deletions

View File

@ -56,13 +56,11 @@ The actual difference of **723 cycles** does not really matter. What is importan
# Summary
## General tips
1. [Use variables as placeholders for constant values](#1-use-variables-as-placeholders-for-constant-values)
Accessing a known value in a variable is faster than deciphering values in code.
2. [Declare your most used variables first](#2-declare-your-most-used-variables-first)
Create and/or reference the variables you're going to use the most as soon as possible
1. [Use variables as placeholders for constant values](#1-use-variables-as-placeholders-for-constant-values): accessing a known value in a variable is faster than deciphering values in code.
2. [Declare your most used variables first](#2-declare-your-most-used-variables-first): create and/or reference the variables you're going to use the most as soon as possible
## Calculations
3. [Use addition instead of multiplication by 2](#3-use-addition-instead-of-multiplication-by-2) Addition of the same variable twice is faster than multiplying the variable by 2
3. [Use addition instead of multiplication by 2](#3-use-addition-instead-of-multiplication-by-2): addition of the same variable twice is faster than multiplying the variable by 2
(and many others) coming soon...
# General Tips
@ -272,7 +270,9 @@ The same kind of process should be made with the variable ``U``. Should it be de
# Calculations
## 3) Use addition instead of multiplication by 2
Multiplication is just another form of addition. And when multiplying by 2, it's faster to use the addition counterpart. This is **always** true if you use variables and replace hardcoded constants with variables (see section [Use variables as placeholders for constant values](#1-use-variables-as-placeholders-for-constant-values)). If you don't, you might get mitigated results.
Multiplication is just another form of addition. And when multiplying by 2, it's faster to use the addition counterpart.
This is **always** true if what you want to do is ``A=2*B`` and that you use variables and replace hardcoded constants with variables (see section [Use variables as placeholders for constant values](#1-use-variables-as-placeholders-for-constant-values)). If you don't, you might get mitigated results.
Demonstration:
```basic
@ -313,4 +313,39 @@ While line 20 of snippet #2:
```
takes 3287 cycles, that is 51 cycles slower. Of course it gets worse with higher multiplication values.
It's also important to notice that this will work only if you already have in a variable the value you want to double.
Let's consider the following, you want to double the result of another calculation, like a division with code like ``D=2*A/B``
Snippet #1
```basic
10 A=123: B=45: C=2: D=0: E=0
20 E=C*A/B
30 END
```
Line 20 takes 6795 cycles. Notice how line 10 declares five variables ``A-E``. These variables will be used in the subsequent snippets. Declaring them, even though they're not used, allows us to ignore the extra cycles needed to create a new variable.
Now let's try with the addition:
```basic
10 A=123: B=45: C=2: D=0: E=0
20 D=A/B+A/B
30 END
```
Line 20 takes 9072 cycles, which is slower (2277 cycles slower).
Now you might think that storing the result of ``A/B`` would be faster. It's not. Except, maybe if you intend to use that result elsewhere in your code in which case it might be worth to spend those cycles storing a result in a variable.
First snippet demonstrates the speed if you don't care about the result of ``A/B``
```basic
10 A=123: B=45: C=2: D=0: E=0
20 D=A/B: E=D+D
30 END
```
Line 20 takes 7090 cycles, it's 295 cycles slower than using directly ``E=C*A/B``.
This second snippet illustrates the speed if the result of ``A/B`` is of any interest and is meant to be reused several other times: it's thus calculated on line 10 and excluded from cycles count.
```basic
10 A=123: B=45: C=2: D=A/B: E=0
20 E=D+D
30 END
```
line 20 takes only 2409 cycles. Using ``20 E=C*D`` would take 2283 cycles more.