[HP35s] Fastest way to increment a variable - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: General Forum (/forum-4.html) +--- Thread: [HP35s] Fastest way to increment a variable (/thread-12893.html) |
[HP35s] Fastest way to increment a variable - fred_76 - 04-29-2019 03:13 PM 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:
With the slow equations : Code:
The ugly one : Code:
After an idea of Csaba, the counter is stored in the stat pile (to be recalled by the function [SUMS][n]) : Code:
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:
The unexpected but quite fast one : Code:
If you agree to store constants in variables (here 1 in N), then : Code:
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 RE: [HP35s] Fastest way to increment a variable - Albert Chan - 04-29-2019 06:18 PM (04-29-2019 03:13 PM)fred_76 Wrote: The unexpected and quite fast one : 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 ... RE: [HP35s] Fastest way to increment a variable - Csaba Tizedes - 04-29-2019 06:51 PM (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 RE: [HP35s] Fastest way to increment a variable - grsbanks - 04-30-2019 07:23 AM (04-29-2019 06:51 PM)Csaba Tizedes Wrote: 2a.) Please test 0 SUM+ (this increases stat variable n) OR These will both be pretty slow because of the other calculations going on whenever data is added to the statistics. RE: [HP35s] Fastest way to increment a variable - ijabbott - 04-30-2019 07:56 AM (04-29-2019 06:18 PM)Albert Chan Wrote:(04-29-2019 03:13 PM)fred_76 Wrote: The unexpected and quite fast one : 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. RE: [HP35s] Fastest way to increment a variable - Csaba Tizedes - 04-30-2019 08:28 AM (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 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:
BTW: on my 20S 1 STO+ var and 1 SUM+ speed ratio is 2.05, it is good profit for the additional benefits. Csaba RE: [HP35s] Fastest way to increment a variable - fred_76 - 04-30-2019 01:18 PM (04-29-2019 06:51 PM)Csaba Tizedes Wrote: Two notes: Csaba, As the HP35s does not have a timer, I had to measure manually with this code : Code:
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: --- 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:
Code:
Note that Σ+ runs faster when x = 0 (34 ms) and even faster when also y=0 (30 ms) so the CLSTK. Code:
RE: [HP35s] Fastest way to increment a variable - fred_76 - 04-30-2019 01:26 PM (04-29-2019 06:18 PM)Albert Chan Wrote:(04-29-2019 03:13 PM)fred_76 Wrote: The unexpected and quite fast one : 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 RE: [HP35s] Fastest way to increment a variable - burkhard - 04-30-2019 01:49 PM (04-30-2019 01:18 PM)fred_76 Wrote:(04-29-2019 06:51 PM)Csaba Tizedes Wrote: 1.) How you measured? I had the same question as Csaba and I liked your answer, Fred. The timing aspect was as interesting to me as the incrementation results themselves. Thanks! |