Post Reply 
Python Speed
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
Post Reply 


Messages In This Thread
Python Speed - toml_12953 - 04-21-2021, 07:34 AM
RE: Python Speed - robmio - 04-21-2021, 12:05 PM
RE: Python Speed - toml_12953 - 04-21-2021, 01:46 PM
RE: Python Speed - robve - 04-21-2021, 03:14 PM
RE: Python Speed - txdinh - 04-29-2024, 09:52 PM
RE: Python Speed - komame - 05-02-2024 06:44 AM
RE: Python Speed - txdinh - 05-02-2024, 11:24 AM
RE: Python Speed - Xorand - 04-21-2021, 05:10 PM
RE: Python Speed - toml_12953 - 04-22-2021, 06:26 PM
RE: Python Speed - Guenter Schink - 04-22-2021, 06:41 PM
RE: Python Speed - txdinh - 05-02-2024, 12:20 AM



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