Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:36 28 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : [Picomite] Optimizing for speed

     Page 1 of 2    
Author Message
fmafma

Newbie

Joined: 30/06/2023
Location: France
Posts: 31
Posted: 08:02am 04 Jul 2023
Copy link to clipboard 
Print this post

Hi!

Do you have some tricks to optimize a program for speed?

For example, I found:

- use integers instead of floats;
- use SELECT CASE instead of IF / ELSEIF;
- use INC <var>, <increment> instead of <var> = <var> + <increment>;

What else?

Thanks,
Frédéric
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 08:12am 04 Jul 2023
Copy link to clipboard 
Print this post

Keep interrupts as short as possible.
IDEALLY, set a flag, and then IMMEDIATELY exit the interrupt.
Have the main loop check the flags and action.

A VERY common mistake, is to put the code you WANT to execute, inside the interrupt, which is a very bad idea.  I know, cos I did it, and learned the hard way.  

"Thou shalt not hang around in interrupts.".....
Smoke makes things work. When the smoke gets out, it stops!
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2136
Posted: 08:28am 04 Jul 2023
Copy link to clipboard 
Print this post

The MATH commands process arrays very fast.

Use the minimum number of characters possible.
eg
Single letter variable names, Pin numbers not GPx numbers
No extra spaces in a line
Cram as many commands on a line as you can
Short FOR and DO loops on one line
> Dim integer n,a(100):For n=0 to 99:a(n)=Pin(1):next

Above all overclock, my Picos can all go to 378MHz.

Edit
the above for - next loop took less than 2mS at 378MHz.
> timer=0:For n=0 to 99:a(n)=Pin(1):next:?timer
1.72
>
Edited 2023-07-04 18:46 by phil99
 
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 207
Posted: 09:15am 04 Jul 2023
Copy link to clipboard 
Print this post

  fmafma said  Hi!

Do you have some tricks to optimize a program for speed?

For example, I found:

- use integers instead of floats;
- use SELECT CASE instead of IF / ELSEIF;
- use INC <var>, <increment> instead of <var> = <var> + <increment>;

What else?

Thanks,


In a FOR i = 1 TO n
<do something>
NEXT

don't write NEXT i - write NEXT without i


Example:
with i runtime:  50356.933
without i runtime:  44866.376
> list

t = Timer
dummy = 0
For i = 1 To 1000000 '(one million)
 dummy = dummy+1
Next i

Print "with i runtime: ";Timer -t

t = Timer
dummy = 0
For i = 1 To 1000000 '(one million)
 dummy = dummy+1
Next

Print "without i runtime: ";Timer -t
>

-andreas
Edited 2023-07-04 19:22 by andreas
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2076
Posted: 09:20am 04 Jul 2023
Copy link to clipboard 
Print this post

explore CONSTs and radices (&h, &b etc). I covered this in my tips page (beware this is aimed at MicroMite so not everything will be applicable)

http://www.fruitoftheshed.com/MMBasic.Some-Hints-Tips-for-writing-efficient-code.ashx
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1114
Posted: 10:20am 04 Jul 2023
Copy link to clipboard 
Print this post

  fmafma said  
- use SELECT CASE instead of IF / ELSEIF;
- use INC <var>, <increment> instead of <var> = <var> + <increment>;

What else?

Thanks,


instead of

if Deg >359 then Deg=Deg-360
if Deg<0 then Deg=Deg+360

use
inc deg,-360*(deg>359)+360*(deg<0)
'no comment
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9129
Posted: 10:26am 04 Jul 2023
Copy link to clipboard 
Print this post

deg=(deg + 360) mod 360
Edited 2023-07-04 20:28 by matherp
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 11:05am 04 Jul 2023
Copy link to clipboard 
Print this post

  CaptainBoing said  my tips page ... http://www.fruitoftheshed.com/MMBasic.Some-Hints-Tips-for-writing-efficient-code.ashx


Cap'n--I can't access fruitoftheshed any longer--I just get a blank page. This occurs with Chrome, Iridium, Edge, and Brave browsers, and also on my phone.

This appears for me to be related to http vs https--some time ago chrome stopped working but iridium still did. Any ideas? Anyone else having the same problem?

~
Edited 2023-07-04 21:05 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1114
Posted: 11:34am 04 Jul 2023
Copy link to clipboard 
Print this post

  matherp said  deg=(deg + 360) mod 360
     

what I wanted to show was that logical comparisons can be included in the formula
Edited 2023-07-04 21:36 by Martin H.
'no comment
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3804
Posted: 12:02pm 04 Jul 2023
Copy link to clipboard 
Print this post

  lizby said  
  CaptainBoing said  my tips page ... http://www.fruitoftheshed.com/MMBasic.Some-Hints-Tips-for-writing-efficient-code.ashx


Cap'n--I can't access fruitoftheshed any longer--I just get a blank page. This occurs with Chrome, Iridium, Edge, and Brave browsers, and also on my phone.

This appears for me to be related to http vs https--some time ago chrome stopped working but iridium still did. Any ideas? Anyone else having the same problem?

~

I can see it OK. Sorry, I don't know what else you can try other than clearing browser cache / reboot / wonder if ISP has (bizarrely) blocked the site.

John
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4247
Posted: 12:36pm 04 Jul 2023
Copy link to clipboard 
Print this post

  matherp said  deg=(deg + 360) mod 360


DEG() is a function. Name a variable same is not recommended.
PicomiteVGA PETSCII ROBOTS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 12:40pm 04 Jul 2023
Copy link to clipboard 
Print this post

FoTS seems to be working here. I blame the US. ;)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1114
Posted: 01:02pm 04 Jul 2023
Copy link to clipboard 
Print this post

  Volhout said  
  matherp said  deg=(deg + 360) mod 360


DEG() is a function. Name a variable same is not recommended.


of course you're right, it was only meant as an example
'no comment
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 01:48pm 04 Jul 2023
Copy link to clipboard 
Print this post

Use if a=1 then if b=2 then
instead of
if a=1 and b=2 then
if the 1st condition fails it skips the 2nd test

use var=not var   to toggle a var

2 code examples, they do the same but one uses 3 arrays to do the same.
cdy(cm)=(py>cy(cm))-(py<cy(cm))  and cdx(cm)=(px>cx(cm))-(px<cx(cm)) speed it,
or do they?
it's computer y direction=(player y>computer y)-(player y<computer y)
and computer x direction=(player x>computer x)-(player x<computer x)
is this safe logic?
 
'computer move
   for cm=1 to 3
     if cpa(cm)=1 then
       if cdx(cm)<>0 then
         if pixel (cx(cm)+cdx(cm),cy(cm)) >0 then
           cdx(cm)=0:cdy(cm)=(py>computer y direction=(player y>computer y)-(player y<computer y)cy(cm))-(py<cy(cm))
         end if
       else
         if pixel (cx(cm),cy(cm)+cdy(cm)) >0 then
           cdy(cm)=0:cdx(cm)=(px>cx(cm))-(px<cx(cm))
         end if
       end if  
       cx(cm)=cx(cm)+cdx(cm):cy(cm)=cy(cm)+cdy(cm)
    next


'computer move
if cdx%=-1 then
  if pixel (cx%-1,cy%) >0 then
    cdx%=0
    if py%>cy% then
      cdy%=1
    else
      cdy%=-1
    end if
  end if
else if cdx%=1 then
  if pixel (cx%+1,cy%) >0 then
    cdx%=0
    if py%>cy% then
      cdy%=1
    else
      cdy%=-1
    end if  
  end if
else if cdy%=-1 then
  if pixel (cx%,cy%-1) >0 then
    cdy%=0
    if px%>cx% then
      cdx%=1
    else
      cdx%=-1
    end if  
  end if
else if cdy%=1 then
  if pixel (cx%,cy%+1) >0 then
    cdy%=0
    if px%>cx% then
      cdx%=1
    else
      cdx%=-1
    end if  
  end if
end if
cx%=cx%+cdx%:cy%=cy%+cdy%
Edited 2023-07-05 01:26 by stanleyella
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1114
Posted: 03:40pm 04 Jul 2023
Copy link to clipboard 
Print this post

  matherp said  deg=(deg + 360) mod 360

yes

> option cpuspeed 378000
> RUN
IF  201.248
MOD  177.022
Calc  223.552
> list
de%=0:CLS
t%=Timer
For f=0 To 10000:Inc de%:If de%>359 Then de%=0
Next
Print "IF ";Timer-t%:t%=Timer
For f=0 To 10000:Inc de%:de%=(de% + 360) Mod 360:Next
Print "MOD ";Timer-t%:t%=Timer
For f=0 To 10000:Inc de%:Inc de%,-360*(de%>359):Next
Print "Calc ";Timer-t%:t%=Timer

the MOD Version is the fastest and to my suprise, the "IF" Version is faster than the Calculated version
'no comment
 
Bleep
Guru

Joined: 09/01/2022
Location: United Kingdom
Posts: 511
Posted: 04:37pm 04 Jul 2023
Copy link to clipboard 
Print this post

Hi Martin,
You are only doing half of the If comparison, what about the
If Deg<0 then Deg=Deg+360
?
Edited 2023-07-05 02:39 by Bleep
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 04:50pm 04 Jul 2023
Copy link to clipboard 
Print this post

  phil99 said  The MATH commands process arrays very fast.
>

 math scale samples!(),80,samples!()

 math add samples!(),0,old_samples!() 'copies a() to b()
 
not obvious this just copies with 0 but a good tip
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9129
Posted: 04:55pm 04 Jul 2023
Copy link to clipboard 
Print this post

  Quote  not obvious this just copies with 0 but a good tip


Check the manual entries for MATH SET, MATH SCALE and MATH ADD.
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2076
Posted: 04:58pm 04 Jul 2023
Copy link to clipboard 
Print this post

  lizby said  
  CaptainBoing said  my tips page ... http://www.fruitoftheshed.com/MMBasic.Some-Hints-Tips-for-writing-efficient-code.ashx


Cap'n--I can't access fruitoftheshed any longer--I just get a blank page. This occurs with Chrome, Iridium, Edge, and Brave browsers, and also on my phone.

This appears for me to be related to http vs https--some time ago chrome stopped working but iridium still did. Any ideas? Anyone else having the same problem?

~


check your DMs
 
hhtg1968
Senior Member

Joined: 25/05/2023
Location: Germany
Posts: 123
Posted: 05:27pm 04 Jul 2023
Copy link to clipboard 
Print this post

  andreas said  
  fmafma said  Hi!

Do you have some tricks to optimize a program for speed?

For example, I found:

- use integers instead of floats;
- use SELECT CASE instead of IF / ELSEIF;
- use INC <var>, <increment> instead of <var> = <var> + <increment>;

What else?

Thanks,


In a FOR i = 1 TO n
<do something>
NEXT

don't write NEXT i - write NEXT without i


Example:
with i runtime:  50356.933
without i runtime:  44866.376
> list

t = Timer
dummy = 0
For i = 1 To 1000000 '(one million)
 dummy = dummy+1
Next i

Print "with i runtime: ";Timer -t

t = Timer
dummy = 0
For i = 1 To 1000000 '(one million)
 dummy = dummy+1
Next

Print "without i runtime: ";Timer -t
>

-andreas



very cool tip. first i do not believe it.
 
     Page 1 of 2    
Print this page
© JAQ Software 2024