# HP Forums

Full Version: [HP35s] Fastest way to increment a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

There are several ways to increment an integer variable by 1. But some are faster than others.

First of all, the "most common one", which is the base time :
Code:
``` 1 STO+V```
=> takes 37 ms and 2 lines of code

With the slow equations :
Code:
``` EQN 1+V►V```
=> takes 125 ms (3.4x slower) but only 1 line of code

The ugly one :
Code:
``` RCL V 1 + STO V```
=> takes 49 ms (1.3x slower) and 4 lines of code

After an idea of Csaba, the counter is stored in the stat pile (to be recalled by the function [SUMS][n]) :
Code:
``` CLSTK Σ+```
=> takes 37 ms (as fast as the base case), and 2 lines of code, but requires the use of the stat pile and clears the xyzt pile

Note that the CLSTK is required for time effectiveness as Σ+ runs faster when the registers x=y=0. If you want to be more friendly with the pile, use CLx instead of CLSTK, you will loose about 2 ms but it will be better for the pile's health !

If you don't use CLSTK or CLx, the execution time of Σ+ with x,y<>0,0 will take about 42 ms.

The official alternative :
Code:
``` ISG V Dummy line```
=> takes 31 ms (1.2x faster) and 2 lines of code

The unexpected but quite fast one :
Code:
``` RCL V NOT +/- STO V```
=> only takes 22 ms (1.7x faster) but 4 lines of code, and only works with integers

If you agree to store constants in variables (here 1 in N), then :
Code:
``` RCL N STO+V```
=> only takes 14 ms (2.6x faster) and 2 lines of code, but requires to store the constant 1 in a variable

These are some ways to optimize the execution time on slow machines, like the HP35s, especially if those instructions are repeated hundreds of times.

As a conclusion :
RCL N | STO+V = 14 ms
RCL V | NOT | +/- | STO V = 22 ms
ISG V | Dummy line = 31 ms
CLSTK | Σ+ = 37 ms
1 | STO+V = 37 ms
CLx | Σ+ = 39 ms
Σ+ = 42 ms
RCL V | 1 | + | STO V = 49 ms
EQN 1+V►V = 125 ms

Fred
(04-29-2019 03:13 PM)fred_76 Wrote: [ -> ]The unexpected and quite fast one :
Code:
``` RCL V NOT +/- STO V```
=> only takes 22 ms (1.7x faster) but 4 lines of code, and only works with integers

Is NOT the bitwise negation operator ? (instead of logical NOT, returning 0, 1)
What range of integers until above not work anymore ? Some 2^n powers ?

If the order NOT +/- is switched, does it decrement the number ?
If true, that make the code *very* unreadable ...
(04-29-2019 03:13 PM)fred_76 Wrote: [ -> ]There are several ways to increment an integer variable by 1.
Two notes:
1.) How you measured?
2a.) Please test 0 SUM+ (this increases stat variable n) OR
2b.) 1 SUM+ (this increases stat variable n AND SUMx)

Thanks,
Csaba
(04-29-2019 06:51 PM)Csaba Tizedes Wrote: [ -> ]2a.) Please test 0 SUM+ (this increases stat variable n) OR
2b.) 1 SUM+ (this increases stat variable n AND SUMx)

These will both be pretty slow because of the other calculations going on whenever data is added to the statistics.
(04-29-2019 06:18 PM)Albert Chan Wrote: [ -> ]
(04-29-2019 03:13 PM)fred_76 Wrote: [ -> ]The unexpected and quite fast one :
Code:
``` RCL V NOT +/- STO V```
=> only takes 22 ms (1.7x faster) but 4 lines of code, and only works with integers

Is NOT the bitwise negation operator ? (instead of logical NOT, returning 0, 1)

Yes, 36-bit bitwise negation.

Quote:What range of integers until above not work anymore ? Some 2^n powers ?

2's complement range [-(2^35), 2^35-1].

Quote:If the order NOT +/- is switched, does it decrement the number ?

Yes. E.g.: "0 +/- NOT" produces -1.
(04-30-2019 07:23 AM)grsbanks Wrote: [ -> ]
(04-29-2019 06:51 PM)Csaba Tizedes Wrote: [ -> ]2a.) Please test 0 SUM+ (this increases stat variable n) OR
2b.) 1 SUM+ (this increases stat variable n AND SUMx)

These will both be pretty slow because of the other calculations going on whenever data is added to the statistics.

Yes, it is obvious, but with this you can add and count in one step and you have a possibility to count back and stop without counter==zero condition checking (eg. with an error):

Code:
``` LBL label   // do something here, which you think is important in your life   SUM-   LN GTO label```

BTW: on my 20S 1 STO+ var and 1 SUM+ speed ratio is 2.05, it is good profit for the additional benefits.

Csaba
(04-29-2019 06:51 PM)Csaba Tizedes Wrote: [ -> ]Two notes:
1.) How you measured?

Csaba,

As the HP35s does not have a timer, I had to measure manually with this code :

Code:
``` Z001 LBL Z Z002 500               *** number of loops, 500 gives a reasonable time ranging from 22 s to about 2 minutes for the slowest functions Z003 STO I Z004 123456          *** number entered in accordance to the function to test Z005 STO X Z006 654321          *** number entered in accordance to the function to test Z007 STO Y Z008 RCL X            *** loop to be repeated 500 times Z009 RCL Y Z010 ........            *** I enter here the function to test Z011 DSE I Z012 GTO Z008 Z013 RTN```

I first measured the time for the empty loop, 10 times and I retain the median time = T0 (=21.80 s)

I then measure the time for the tested function, 5 times, retaining the median time = Tf

The execution time for the tested function is then :

Tef = (Tf-T0)/500*1000 in ms

For example :
function = x²
- Tf = 27.07 s >> Tef = (27.07-21.80)/500*1000=10.54 ms

(04-29-2019 06:51 PM)Csaba Tizedes Wrote: [ -> ]Two notes:
2a.) Please test 0 SUM+ (this increases stat variable n) OR
2b.) 1 SUM+ (this increases stat variable n AND SUMx)

--- edited ----

[SUM+] is [Σ+] on the HP35S and take 42.1 ms to execute. This is far more than [STO+ var] with 9.1 ms. To recall the number of samples from the stat pile, we have to call the function [n] which takes 4.8 ms to execute, a bit faster than [RCL var] with 5.1 ms.

I tested

Code:
``` 0 Σ+```
Time : 63 ms (1.7x slower)

Code:
``` CLSTK Σ+```
Time : 37 ms (as fast as the base case)
Note that Σ+ runs faster when x = 0 (34 ms) and even faster when also y=0 (30 ms) so the CLSTK.

Code:
``` 1 Σ+```
Time : 69 ms (1.9 slower)
(04-29-2019 06:18 PM)Albert Chan Wrote: [ -> ]
(04-29-2019 03:13 PM)fred_76 Wrote: [ -> ]The unexpected and quite fast one :
Code:
``` RCL V NOT +/- STO V```
=> only takes 22 ms (1.7x faster) but 4 lines of code, and only works with integers

What range of integers until above not work anymore ? Some 2^n powers ?

On the HP35s, it works until you overflow the precision of 12 digits, ie within the range of :
-999 999 999 999 ; +999 999 999 999
(04-30-2019 01:18 PM)fred_76 Wrote: [ -> ]
(04-29-2019 06:51 PM)Csaba Tizedes Wrote: [ -> ]1.) How you measured?

As the HP35s does not have a timer, I had to measure manually with this code :