PDA

View Full Version : Measurement time of a peace of code



gadelhas
- 17th August 2011, 18:53
Hi Everyone;

How do you guys measyre the time that takes to execute a piece of code.

I have a piece of code, of a PI outine, and i want to measure how much time it takes to complete so a can achive a good sample rate, howaver i don't know how much time it takes to complete. Can somebody help here;

Here is the code



PI:
GetADC:
K var byte: K = 0
for K = 1 to 20
ADCIN 0,actual_speed
sample = actual_speed + sample
next K
actual_speed = sample/20
sample=0

calculate_error:
error = setPOINT - actual_speed
calculate_proportional:
P = Kp * error

calculate_integral:
error_sum = error_sum + error
sign = error_sum
GOSUB SetSign
IF ABS error_sum > (250*Ki) THEN error_sum = sign * (250*Ki)
I = ABS error_sum / Ki
I = sign * I

calculate_drive:
power = P + I
sign = power
GOSUB SetSign
IF sign = 1 THEN
IF power > 255 THEN power = 255
ELSE
power = 0
ENDIF

HPWM 1,power,30000
RETURN

SetSign:
IF sign.Bit15 = 0 THEN
sign = 1
ELSE
sign = -1
ENDIF
RETURN

HenrikOlsson
- 17th August 2011, 19:02
Hi,
I just use a timer.

Time VAR WORD

TMR1L = 0
TMR1H = 0

T1CON.0 = 1 'Start TMR1
'code here
'code here
T1CON.0 = 0 'Stop TMR1

Time.HighByte = TMR1H
Time.LowByte = TMR1L
HSEROUT ["Execution time in cycles: ", DEC Time,13]

Something like that though I'm sure I've missed a bracket somewhere.... :-)

/Henrik.

mister_e
- 17th August 2011, 19:04
MPLAB stopwatch also do the trick pretty well

If you have a Scope, set a pin high before the section of code you want measure, and clear it at thye end. not as accurate as Timer or StopWatch but easy easy easy :D

gadelhas
- 17th August 2011, 19:15
Something like that though I'm sure I've missed a bracket somewhere.... :-)


Ehehe, this time i don't think so!!! Can you tell me hoe do i do the math so i can have a value in seconds or miliseconds.
I'm using a 8Mhz osc, so with Fosc/a i get 2Mhz, so 0.5uS for cycle. I must multiply this value for the value that i've got in the time var?
Thank you Henrik



MPLAB stopwatch also do the trick pretty well

If you have a Scope, set a pin high before the section of code you want measure, and clear it at thye end. not as accurate as Timer or StopWatch but easy easy easy :D

I'm using MCS, i'm not used to use MPLAB, i think that way with the scope its better. Thank you Steve

gadelhas
- 17th August 2011, 19:35
Update;

I made this code



Main:
if Bmais = 0 then
pause 50
Time VAR WORD
TMR1L = 0
TMR1H = 0
T1CON.0 = 1 'Start TMR1
'Inserir rotina a medir
GOSUB PI
T1CON.0 = 0 'Stop TMR1
Time.HighByte = TMR1H
Time.LowByte = TMR1L
ENDIF

lcdout $FE, 1, " Medidor ciclos "
lcdout $FE,$C0,"N. Ciclos: ", #time
Pause 100
GOTO Main


When i press Bmais, i get always 7203 cycles, so multiply by 0.5uS, i get 0.003602 S, or 3.6mS. Is this calculations correct?

mister_e
- 17th August 2011, 19:38
maybe... replace the gosub by a pause 10, and see what happen ;)

gadelhas
- 17th August 2011, 19:42
maybe... replace the gosub by a pause 10, and see what happen ;)

I get always 20009 cycles, so multiply by 0.5uS, i get 0.010005S, or 10mS. So i assume that is correct, and works great.

Thank you Henrik and Steve!

HenrikOlsson
- 17th August 2011, 19:42
Yes exactly, in this case it's just a matter of multiplying the value you get with 0.5 to get the answer in uS.

If you want to do that in PBP then perhaps:


' In us
LCDOUT $FE, 1, ["Timer ticks: ", DEC Time]
LCDOUT $FE, $C0, ["Execution time: ", DEC Time/2, "." DEC Time//2, "us",13]

I think that should do it.

Bruce
- 17th August 2011, 19:47
DT has a nice example here: http://www.picbasic.co.uk/forum/showthread.php?t=365&p=1272#post1272

Something else to consider is where the code you're timing is located. See the 1st paragraph.

gadelhas
- 17th August 2011, 20:29
I got it. Thank you all, once again!

ScaleRobotics
- 17th August 2011, 20:32
SteveB had a nice include file called codetimer.bas from here: http://www.picbasic.co.uk/forum/showthread.php?t=4884&p=27197#post27197 for PIC18 devices.

gadelhas
- 17th August 2011, 21:13
SteveB had a nice include file called codetimer.bas from here: http://www.picbasic.co.uk/forum/showthread.php?t=4884&p=27197#post27197 for PIC18 devices.

WoW, thast awesome, very good indeed!