20M2 Loop Speed slower than expected

Armp

Senior Member
I wrote some code to generate test signals, using the 'rule of thumb' 250uS per op - but it ran much slower than I expected.
Breaking the code down to understand the bottleneck, I found that an 'empty' FOR/NEXT loop takes 1.6mS!
Are there any techniques to 'loop' faster than this?

Switched to 32Mhz, and it comes down to 0.2mS of course - but that's not the issue.

Code:
#picaxe 20M2
#no_data
SetFreq m32

Main:
Do
  For b0 = 0 to 23
  'Stuff commented out for this test.
  Next b0

 Toggle B.7     	' Sync Pulse  
Loop 		'Loops about 40mS @ 4Mhz,  5mS @ 32Mhz
I searched on 'loop speed' and got 500 threads. I know there's lots of good information in this forum, but the search feature is off little value IMO.
 

westaust55

Moderator
Have you had had a read of the artice I posted here: http://www.picaxeforum.co.uk/showthread.php?17782-PICAXE-program-Size-Optimisations-and-Speed

The table on page 12 gives some insight.
The newer X2 and M2 parts have slightly greater overheads as a function of extra features.
For the 18M2 and 28X2 you can see that time to perform a command is around 25% and 50% longer.
Hence as a guide, instead of 250 us average per command, consider say 310 us for M2 parts and 390 or 400 us for X2 parts operating at 4 MHz.

Commands such as HIGH, LOW, TOGGLE, PAUSE, and simple math assignment do have the above durations, but IF...THEN take double to treble this time duration.

For an 08M, an empty FOR...NEXT loop run 65,535 times in effect gave an average time of 1 ms per loop.
For a 40X1, the same loop required 1.14ms per loop.

Keep in mind that the FOR...NEXT loop introduces time requirements to set up the index variable, increment the variable once per loop and divert the operation back to the start of the loop until complete. Plus there is time required to check for interrupts, background receive (where applicable), etc
 
Last edited:

Armp

Senior Member
I have now :) Thanks.

It would seem that a more realistic 'rule of thumb' for me - based on my mix of operations, loops, read, daclevel - would be about 600us/op at 4Mhz. About twice my initial assumption.

EDIT - Maybe that's bit pessimistic. Womai says 500uS.

Rough rule of thumb: Non-X2 Picaxes approx. 2000 commands per sec at 4 MHz. X2 Picaxes approx. 3000 command per sec at 8 MHz. This of course only applies to command who don't e.g. wait for some event (pulsin for example) of have to do a lot of processing (e.g. serout of a long byte sequence).
 
Last edited:
Top