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: FrancePosts: 31 |
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 ZealandPosts: 9308 |
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: AustraliaPosts: 2137 |
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: GermanyPosts: 207 |
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 KingdomPosts: 2076 |
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: GermanyPosts: 1114 |
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 KingdomPosts: 9129 |
deg=(deg + 360) mod 360 Edited 2023-07-04 20:28 by matherp |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3150 |
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: GermanyPosts: 1114 |
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 KingdomPosts: 3804 |
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: NetherlandsPosts: 4247 |
DEG() is a function. Name a variable same is not recommended. PicomiteVGA PETSCII ROBOTS |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6798 |
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: GermanyPosts: 1114 |
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 KingdomPosts: 2129 |
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: GermanyPosts: 1114 |
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 KingdomPosts: 512 |
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 KingdomPosts: 2129 |
> 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 KingdomPosts: 9129 |
Check the manual entries for MATH SET, MATH SCALE and MATH ADD. |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2076 |
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: GermanyPosts: 123 |
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 |