Post Reply 
Python Speed
04-21-2021, 07:34 AM (This post was last modified: 04-21-2021 07:39 AM by toml_12953.)
Post: #1
Python Speed
The new Python interpreter graphics are fast!

I ran three programs to draw a green hat, Here are the runtimes:

HPPL: 7.793 sec
CAS with Python syntax: 25.436 sec
Python (Apr 16 Beta): 0.926 sec

Here's the Python version:
Code:
#PYTHON EXPORT pyhat
from hpprime import *
from math import *
t0 = eval("ticks()") # Save the current clock count for timing program
# Clear screen
fillrect(0,0,0,320,240,0,0)
# Start program proper
p=160; q=120
xp=144; xr=1.5*3.1415927
yp=56; yr=1; zp=64
xf=xr/xp; yf=yp/yr; zf=xr/zp
for zi in range(-q,q+1):
  if zi>=-zp and zi<=zp:
    zt=zi*xp/zp; zz=zi
    xl=int(.5+sqrt(xp*xp-zt*zt))
    # Draw one cross-section of figure
    for xi in range(-xl,xl+1):
      xt=sqrt(xi*xi+zt*zt)*xf; xx=xi
      yy=(sin(xt)+.4*sin(3*xt))*yf
      x1=xx+zz+p
      y1=yy-zz+q
      pixon(0,x1,230-y1,65280)
      if y1!=0:
        line(0,x1,230-y1+1,x1,230,0) # Erase points below current point
t = eval("ticks()")-t0
# Wait for key and print elapsed time
eval("wait()")
t = t/1000
print(t," seconds")
#end

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
04-21-2021, 12:05 PM
Post: #2
RE: Python Speed
Incredible! How was this speed test done? Did you use the emulator? Have you used the HP PRIME G2?
Find all posts by this user
Quote this message in a reply
04-21-2021, 01:46 PM
Post: #3
RE: Python Speed
(04-21-2021 12:05 PM)robmio Wrote:  Incredible! How was this speed test done? Did you use the emulator? Have you used the HP PRIME G2?

All times were measured on a G2.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
04-21-2021, 03:14 PM
Post: #4
RE: Python Speed
(04-21-2021 07:34 AM)toml_12953 Wrote:  The new Python interpreter graphics are fast!

I ran three programs to draw a green hat, Here are the runtimes:

HPPL: 7.793 sec
CAS with Python syntax: 25.436 sec
Python (Apr 16 Beta): 0.926 sec

Very nice. My PRIME G2 takes even a bit longer, about 8.2 seconds to run the HPPL program.

I've used the following code, which I've translated to HPPL from the CAS Python code:

Code:
EXPORT pyhat()
BEGIN
  LOCAL XP,XR,YP,YR,ZP,XI;
  LOCAL XF,YF,ZF,ZI,ZT,ZZ;
  LOCAL XL,XT,XX,YY,X1,Y1;
  T:=TICKS; // Save the current clock count for timing program
  // Black out background
  rect_p(0,0,320,240,rgb(0,0,0));
  // Start program proper
  P:=160; Q:=120;
  XP:=144; XR:=1.5*3.1415927;
  YP:=56; YR:=1; ZP:=64;
  XF:=XR/XP; YF:=YP/YR; ZF:=XR/ZP;
  FOR ZI FROM -Q TO Q DO
    IF ZI>=-ZP AND ZI<=ZP THEN
      ZT:=ZI*XP/ZP; ZZ:=ZI;
      XL:=int(.5+sqrt(XP*XP-ZT*ZT));
      // Draw one cross-section of figure
      FOR XI FROM -XL TO XL DO
        XT:=sqrt(XI*XI+ZT*ZT)*XF; XX=XI;
        YY:=(sin(XT)+.4*sin(3*XT))*YF;
        X1:=XX+ZZ+P;
        Y1:=YY-ZZ+Q;
        pixon_p(X1,230-Y1,rgb(0,255,0));
        IF Y1!=0 THEN
          Line_p(X1,230-Y1+1,X1,230); // Erase points below current point
        END;
      END;
    END;
  END;
  T:=TICKS-T;
  // Wait for key and print elapsed time
  FREEZE;
  PRINT((T/1000)+" seconds");
END;

Not sure if the PRIME Python graphics library internals are any different. The Python interpreter presumably uses the hardware floating point support of the Cortex-A7. The fp performance of Python on the HP PRIME is incredibly good: https://tiplanet.org/forum/viewtopic.php...518#HPPPY5 (translated in English with Google translate):

"The HP Prime are really extraordinary in floating point calculation Python and the HP Prime G2 even crushes all competition with its omnipotence, even by doping the latter with overclocking! Both among the solutionsPython official and solutions Python compatible with exam mode, HP Prime G2 is clearly the most powerful either in integer or in floating point calculus, and by far, congratulations!"

Even faster than "doping the competition with overclocking"... Wink

- Rob

"I count on old friends to remain rational"
Visit this user's website Find all posts by this user
Quote this message in a reply
04-21-2021, 05:10 PM
Post: #5
RE: Python Speed
That's blindingly fast.

As a point of comparison, I adapted your python program to run on my TI Nspire CX II CAS under its native python.

The program took 215.98 seconds (yes, 3.5 minutes) to draw the hat.

I optimized it slightly by doing all the drawing in a buffer and displaying the final product when done. That reduced time to 82.25 seconds.

Looks like the HP Prime python is very well optimized.
Find all posts by this user
Quote this message in a reply
04-22-2021, 06:26 PM
Post: #6
RE: Python Speed
Changing

Code:
pixon(0,x1,230-y1,65280)

to

Code:
pixon(0,x1,230-y1,eval("rgb(0,255,0)"))

made the runtime go from 0.92 seconds to 4.37 seconds!

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
04-22-2021, 06:41 PM
Post: #7
RE: Python Speed
Although I think that the "Summation based benchmark for calculators " is of limited value, I thought I give it a try. Look here for the results - impressive.

Günter
Find all posts by this user
Quote this message in a reply
04-29-2024, 09:52 PM
Post: #8
RE: Python Speed
(04-21-2021 03:14 PM)robve Wrote:  
(04-21-2021 07:34 AM)toml_12953 Wrote:  The new Python interpreter graphics are fast!

I ran three programs to draw a green hat, Here are the runtimes:

HPPL: 7.793 sec
CAS with Python syntax: 25.436 sec
Python (Apr 16 Beta): 0.926 sec

Very nice. My PRIME G2 takes even a bit longer, about 8.2 seconds to run the HPPL program.

I've used the following code, which I've translated to HPPL from the CAS Python code:

Code:
EXPORT pyhat()
BEGIN
  LOCAL XP,XR,YP,YR,ZP,XI;
  LOCAL XF,YF,ZF,ZI,ZT,ZZ;
  LOCAL XL,XT,XX,YY,X1,Y1;
  T:=TICKS; // Save the current clock count for timing program
  // Black out background
  rect_p(0,0,320,240,rgb(0,0,0));
  // Start program proper
  P:=160; Q:=120;
  XP:=144; XR:=1.5*3.1415927;
  YP:=56; YR:=1; ZP:=64;
  XF:=XR/XP; YF:=YP/YR; ZF:=XR/ZP;
  FOR ZI FROM -Q TO Q DO
    IF ZI>=-ZP AND ZI<=ZP THEN
      ZT:=ZI*XP/ZP; ZZ:=ZI;
      XL:=int(.5+sqrt(XP*XP-ZT*ZT));
      // Draw one cross-section of figure
      FOR XI FROM -XL TO XL DO
        XT:=sqrt(XI*XI+ZT*ZT)*XF; XX=XI;
        YY:=(sin(XT)+.4*sin(3*XT))*YF;
        X1:=XX+ZZ+P;
        Y1:=YY-ZZ+Q;
        pixon_p(X1,230-Y1,rgb(0,255,0));
        IF Y1!=0 THEN
          Line_p(X1,230-Y1+1,X1,230); // Erase points below current point
        END;
      END;
    END;
  END;
  T:=TICKS-T;
  // Wait for key and print elapsed time
  FREEZE;
  PRINT((T/1000)+" seconds");
END;

Not sure if the PRIME Python graphics library internals are any different. The Python interpreter presumably uses the hardware floating point support of the Cortex-A7. The fp performance of Python on the HP PRIME is incredibly good: https://tiplanet.org/forum/viewtopic.php...518#HPPPY5 (translated in English with Google translate):

"The HP Prime are really extraordinary in floating point calculation Python and the HP Prime G2 even crushes all competition with its omnipotence, even by doping the latter with overclocking! Both among the solutionsPython official and solutions Python compatible with exam mode, HP Prime G2 is clearly the most powerful either in integer or in floating point calculus, and by far, congratulations!"

Even faster than "doping the competition with overclocking"... Wink

- Rob

Thanks Rob for the HPPL code !!
Although 3 minor changes are needed for those who are interested to try it out:
- replace int() with IP(), since int() is an integral function
- replace XX=XI with XX:=XI, just a typo
- replace FREEZE with WAIT(), not sure why FREEZE didn't wait for a key press
or else I get no hat or a 2d hat Smile

I need to develop a HP Prime program to use for work related tasks and I have been thinking about Python after many years from HP-25 to HP48*/HP-50. Smile
I ran both Python hat program from Tom (3s) and HPPL hat program from Rob (25s) on the HP Prime G1 and the HPPL version produced a lot more detailed plot for some reason ?? although the 2 codes looks very similar line by line. If anybody knows why the Python vs HPPL hat plots resolution are so different please post your finding here. This might have contributed to the significant additional execution time with HPPL.

Thanks!!
Find all posts by this user
Quote this message in a reply
05-02-2024, 12:20 AM
Post: #9
RE: Python Speed
I think I found the issue ... The hidden lines were not removed in the HPPL plot and added the additional details to the plot. This was because "Y1!=0" was incorrect syntax in HPPL and should be "Y1=/=0" instead for the line_p() to execute. It still took 25s to plot on the HP Prime G1 nonetheless.
Find all posts by this user
Quote this message in a reply
05-02-2024, 06:44 AM (This post was last modified: 05-02-2024 07:07 AM by komame.)
Post: #10
RE: Python Speed
(04-29-2024 09:52 PM)txdinh Wrote:  I ran both Python hat program from Tom (3s) and HPPL hat program from Rob (25s) on the HP Prime G1 and the HPPL version produced a lot more detailed plot for some reason ?? although the 2 codes looks very similar line by line. If anybody knows why the Python vs HPPL hat plots resolution are so different please post your finding here. This might have contributed to the significant additional execution time with HPPL.

Another change is required in the PPL version. The line:
IF Y1!=0 THEN
should look like this:
IF Y1<>0 THEN

This way, the condition compares the factorial of X with zero instead of X being different from zero. This is why the PPL version did not remove invisible pixels (behind the hat). Additionally, it's worth changing RGB to the direct color value (#00FF00h), which slightly speeds up rendering because RGB calculates it each time.

Here's the corrected version:
Code:
EXPORT pplhat()
BEGIN
  LOCAL XP,XR,YP,YR,ZP,XI;
  LOCAL XF,YF,ZF,ZI,ZT,ZZ;
  LOCAL XL,XT,XX,YY,X1,Y1;
  HAngle:=0;
  T:=TICKS; // Save the current clock count for timing program
  // Black out background
  rect_p(0);
  // Start program proper
  P:=160; Q:=120;
  XP:=144; XR:=1.5*3.1415927;
  YP:=56; YR:=1; ZP:=64;
  XF:=XR/XP; YF:=YP/YR; ZF:=XR/ZP;
  FOR ZI FROM -Q TO Q DO
    IF ZI>=-ZP AND ZI<=ZP THEN
      ZT:=ZI*XP/ZP; ZZ:=ZI;
      XL:=IP(.5+sqrt(XP*XP-ZT*ZT));
      // Draw one cross-section of figure
      FOR XI FROM -XL TO XL DO
        XT:=sqrt(XI*XI+ZT*ZT)*XF; XX:=XI;
        YY:=(sin(XT)+.4*sin(3*XT))*YF;
        X1:=XX+ZZ+P;
        Y1:=YY-ZZ+Q;
        pixon_p(X1,230-Y1,#00FF00h);
        IF Y1<>0 THEN
          Line_p(X1,230-Y1+1,X1,230); // Erase points below current point
        END;
      END;
    END;
  END;
  T:=TICKS-T;
  // Wait for key and print elapsed time
  WAIT;
  PRINT((T/1000)+" seconds");
END;

Note that if you remove this condition from the Python version and the line drawing pixels removing those behind the hat, then the Python version speeds up by an additional 30% (2.64s).

Additionally, it's worth noting that MicroPython is not as well optimized for performance as full Python, so in this case, it's worth limiting the number of imported functions from libraries. Therefore, when you replace
from hpprime import *
with
from hpprime import eval,fillrect,pixon,line
you'll gain an additional few percent speedup (the target time on G1 is 2.44s, and on G2 it's just 0.604s).

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
05-02-2024, 11:24 AM
Post: #11
RE: Python Speed
(05-02-2024 06:44 AM)komame Wrote:  
(04-29-2024 09:52 PM)txdinh Wrote:  I ran both Python hat program from Tom (3s) and HPPL hat program from Rob (25s) on the HP Prime G1 and the HPPL version produced a lot more detailed plot for some reason ?? although the 2 codes looks very similar line by line. If anybody knows why the Python vs HPPL hat plots resolution are so different please post your finding here. This might have contributed to the significant additional execution time with HPPL.

Another change is required in the PPL version. The line:
IF Y1!=0 THEN
should look like this:
IF Y1<>0 THEN

This way, the condition compares the factorial of X with zero instead of X being different from zero. This is why the PPL version did not remove invisible pixels (behind the hat). Additionally, it's worth changing RGB to the direct color value (#00FF00h), which slightly speeds up rendering because RGB calculates it each time.

Here's the corrected version:
Code:
EXPORT pplhat()
BEGIN
  LOCAL XP,XR,YP,YR,ZP,XI;
  LOCAL XF,YF,ZF,ZI,ZT,ZZ;
  LOCAL XL,XT,XX,YY,X1,Y1;
  HAngle:=0;
  T:=TICKS; // Save the current clock count for timing program
  // Black out background
  rect_p(0);
  // Start program proper
  P:=160; Q:=120;
  XP:=144; XR:=1.5*3.1415927;
  YP:=56; YR:=1; ZP:=64;
  XF:=XR/XP; YF:=YP/YR; ZF:=XR/ZP;
  FOR ZI FROM -Q TO Q DO
    IF ZI>=-ZP AND ZI<=ZP THEN
      ZT:=ZI*XP/ZP; ZZ:=ZI;
      XL:=IP(.5+sqrt(XP*XP-ZT*ZT));
      // Draw one cross-section of figure
      FOR XI FROM -XL TO XL DO
        XT:=sqrt(XI*XI+ZT*ZT)*XF; XX:=XI;
        YY:=(sin(XT)+.4*sin(3*XT))*YF;
        X1:=XX+ZZ+P;
        Y1:=YY-ZZ+Q;
        pixon_p(X1,230-Y1,#00FF00h);
        IF Y1<>0 THEN
          Line_p(X1,230-Y1+1,X1,230); // Erase points below current point
        END;
      END;
    END;
  END;
  T:=TICKS-T;
  // Wait for key and print elapsed time
  WAIT;
  PRINT((T/1000)+" seconds");
END;

Note that if you remove this condition from the Python version and the line drawing pixels removing those behind the hat, then the Python version speeds up by an additional 30% (2.64s).

Additionally, it's worth noting that MicroPython is not as well optimized for performance as full Python, so in this case, it's worth limiting the number of imported functions from libraries. Therefore, when you replace
from hpprime import *
with
from hpprime import eval,fillrect,pixon,line
you'll gain an additional few percent speedup (the target time on G1 is 2.44s, and on G2 it's just 0.604s).

Thanks a lot Piotr !!
I also realized about the "!=" versus "<>" after posting and reviewing the code.
Thanks for all the additional tips and suggestions !!
Find all posts by this user
Quote this message in a reply
Post Reply 




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