execution time for code

I expected this code to beep every couple of SECONDS, but it took 10 MINUTES.
Any comments on what was going on ? See attachment.
 
execution time for code
here is the code:
'''' code_test_delay.bas

#com /dev/tty.usbserial-00001004
#picaxe 14m2

symbol wd_larry = w0
symbol wd_num2 = w1
symbol wd_num3 = w2
init_x:
call bip
wd_larry = 0
wd_num2 = 0
wd_num3 = 0
do
inc wd_larry
loop while wd_larry < 63000
do
inc wd_num2
loop while wd_num2 < 63000
do
inc wd_num3
loop while wd_num3 < 63000
goto init_x
''''
bip:
high b.2
pause 50
low b.2
return
 
execution time for code
here is the code:
'''' code_test_delay.bas
Code:
#com  /dev/tty.usbserial-00001004
#picaxe 14m2

symbol wd_larry = w0
symbol wd_num2 = w1
symbol wd_num3 = w2
init_x:
call bip
wd_larry = 0
 wd_num2 = 0
wd_num3 = 0
do
inc wd_larry
loop while wd_larry < 63000	
do
inc wd_num2
loop while wd_num2 < 63000
do
inc wd_num3
loop while wd_num3 < 63000
goto init_x
''''       
bip:
high b.2
pause 50
low b.2
return
 
Last edited by a moderator:

sghioto

Senior Member
Ten minutes sounds about right. You're executing a minimum of 567,000 commands between beeps. At 4 mhz it takes roughly 1ms per command or 1000 commands per second. So 567,000 divided by 1000 equals 576 seconds divided by 60 equals 9 minutes and 27 seconds.

Steve G
 

AllyCat

Senior Member
Hi Larry,

PICaxe uses an Interpreted Basic, which runs at least 400 times slower than Compiled Basic or Assembly Language.

A Conditional Jump (DO ... WHILE) takes nearly 1.3 ms per pass and var = var + 1 (INC) around 600 us, so I'd expect 63,000 iterations to take about 2 minutes. But the "higher level" commands can be more difficult to predict than the raw IF/GOTO etc. structure.

For reasonably predictable time delays you should use PAUSE, SLEEP, NAP, etc., or check the "time" variable (counts in seconds).

Chers, Alan.
 
thnx SGHIOTO I guess I was thinking 250 usec per clock step - but this is what I wanted to hear from u guys
and thnx ALLYCAT I know these PIC chips have been re-engineered (and how I love the PICAXE family !!) - I was worried about
the INTERPRETED aspect and also suspicious of the DO....WHILE thing. I had to share this with my son at
MIT Lincoln Labs but thnx for giving me these explanations. I'm working on a MORSE decoder (which I've sone a couple
of times before) I LOVE to re-invent the wheel ! Larry in Massachusetts USA
 

westaust55

Moderator
@Larry,
You might like to have a read through this thread and attachments:
http://www.picaxeforum.co.uk/showthread.php?17782-PICAXE-program-Size-Optimisations-and-Speed

Earlier PICAXE chips executed a "nominal" command in about 250 usec. Even then some commands took longer.
However as the PICAXE parts (X2 and M2) have more features that need more overhead time to undertake/monitor in the background, the execution times have increased for the same clock frequency.
 

tmfkam

Senior Member
I recently wrote a program to detect obstruction within an IR 'gate', a row of IR LEDs and a matching row of IR phototransistors. The program clocks a pair of 4017 counters to move along the row of IR LEDs and Phototransistors, waiting for a 'detect' signal from the phototransistors in order to trigger an alarm.

This worked very well but seemed a little slow. I tried to optimise my code as much as possible but it still seemed slow, even when running at 32MHz. I then re-wrote the same code for another dialect of basic and compiled it for a raw PIC16F1825, also run at 32MHz. The pinouts of the 14M2 and the 16F1825 are broadly similar, allowing me to use both types of chip in the same circuit board without modifying the design.

On testing the new code I compared the two versions in the same PCB using an oscilloscope. The clock signal frequency from a PicAxe14M2 was 1050kHz, the clock of the PIC16F1825 was 21800kHz. Making the PIC something close to 20 times faster than the PicAxe. Admittedly I did have to slow the PIC loop down a little as the phototransistors took a moment to settle before a reliable reading could be detected so the PIC may have been slightly faster than 20 times. I can't see it being 400 times faster, perhaps the compiler I used was not terribly efficient in compiling to assembler?

Code:
'PIC16F1825 Version
Sub IR_WatchSub
    Do
          For Current = 1 to Scan
              PulseOut Clk, 1 uS	'Clock the 4017
              Wait SettleTime uS	'SettleTime defined as 35
              If Det1=0 Then
                Let OutputTime=Time_Length
                Set_DetectSub
              End If
              If Det2=0 Then
                  Let OutputTime=Time_Length
                  Set_DetectSub
              End If
              If OutputTime > 0 Then
                 Let OutputTime=OutputTime-1
              End If
              If OutputTime=1 Then
                 Clear_DetectSub
              End If
          Next
          PulseOut Rset, 1 uS      'Reset the 4017
    Loop
End Sub

'PicAxe 14M2 Version
IR_Watch:
	Do
		For Current=1 to Scan
			High Clk		'Clock the 4017
			Low Clk		'Finish the clock pulse
			If Det1=0 Then
				Let OutputTime=Time_Length
				'Set Detect Signal
				 GoSub Set_DetectSub
			EndIf
			If Det2=0 Then
				Let OutputTime=Time_Length
				'Set Detect Signal
				 GoSub Set_DetectSub
			EndIf
			If OutputTime > 0 Then
				Dec OutputTime
			EndIf
			If OutputTime=1 Then
				'Clear Detect signal
				 GoSub Clear_DetectSub
			EndIf
		Next Current

		High Rset	'Reset the 4017
		Low Rset	'Remove the reset

	Loop
Return
 
Top