The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
[HP35s] Fastest way to increment a variable
04-29-2019, 03:13 PM (This post was last modified: 05-06-2019 08:56 AM by fred_76.)
Post: #1
[HP35s] Fastest way to increment a variable
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
[HP35s] Fastest way to increment a variable - fred_76 - 04-29-2019 03:13 PM



User(s) browsing this thread: 1 Guest(s)