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

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