Measurement time of a peace of code


Closed Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Measurement time of a peace of code

    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
    Thanks and Regards;
    Gadelhas

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    Hi,
    I just use a timer.
    Code:
    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.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    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
    I'm using MCS, i'm not used to use MPLAB, i think that way with the scope its better. Thank you Steve
    Thanks and Regards;
    Gadelhas

  5. #5
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    Update;

    I made this code

    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?
    Thanks and Regards;
    Gadelhas

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    maybe... replace the gosub by a pause 10, and see what happen
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    Quote Originally Posted by mister_e View Post
    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!
    Thanks and Regards;
    Gadelhas

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    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:
    Code:
    ' 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.

  9. #9
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    DT has a nice example here: http://www.picbasic.co.uk/forum/show...=1272#post1272

    Something else to consider is where the code you're timing is located. See the 1st paragraph.
    Last edited by Bruce; - 17th August 2011 at 19:50.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  10. #10
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    I got it. Thank you all, once again!
    Thanks and Regards;
    Gadelhas

  11. #11
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    SteveB had a nice include file called codetimer.bas from here: http://www.picbasic.co.uk/forum/show...7197#post27197 for PIC18 devices.
    Attached Files Attached Files

  12. #12
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Measurement time of a peace of code

    Quote Originally Posted by scalerobotics View Post
    SteveB had a nice include file called codetimer.bas from here: http://www.picbasic.co.uk/forum/show...7197#post27197 for PIC18 devices.
    WoW, thast awesome, very good indeed!
    Thanks and Regards;
    Gadelhas

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts