Shop OBEX P1 Docs P2 Docs Learn Events
Code Execution Time — Parallax Forums

Code Execution Time

JoeFLJoeFL Posts: 10
edited 2012-12-05 14:48 in General Discussion
Hi, I'm fairly new to Spin and the Prop. I was identifying some executions times in my program and have come
across some code which has varying execution times and I'm wondering why that is?
I've attached the spn file for ease of use. One can use viewport and watch the variable MT1D change every 2 seconds

Thanks for you time

Joe



CON


_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000 'use standard 5MHz clock

OBJ 'includ ViewPort objects:


vp : "Conduit" 'transfers data_ to/from PC


Var
long MT_St,MT,MT1,TBP,data_,MT1D


PUB Detect_Rate


vp.config(string("var:TBP,data_,MT1D"))
vp.share(@TBP,@MT1D) 'share variables

data_ := 18 'sets product delivery rate
TBP := (3600*80_000) / data_ 'TBP Time Between Product in clock tics
' 80_000_000 tics/sec
repeat


if MT_St == 0
MT := cnt 'MT Mark Time, 1st time thru loop
MT_St := 1
if MT_St
MT1 := cnt - MT 'keep looking at MT1
if MT1 => TBP
MT_St := 0
MT1D := MT1 - TBP 'This difference in Time seems to vary quite a bit
waitcnt(2*clkfreq + cnt) 'I don't know why?
data_++
TBP := (3600*80_000) / data_


Time_Calc.spin

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-05 12:41
    There's a slight variation in code execution time for division that depends on the values involved. I believe multiplication is written so its time is data independent. Theoretically there's some slight jitter in execution time from memory synchronization with hub memory, but this is so overwhelmed by other hub memory accesses that I don't think you could actually observe it. I think any variation you observe would have to be due to timing in different execution paths in your own code. In other words, any significant differences in time that you're observing are artifacts of your program and not due to any significant execution time jitter in Spin itself.

    Note that execution time for the same sorts of operations will vary somewhat depending on where the operands come from or, if constants, what their value is. Access to variables takes different amounts of storage and time depending on whether the data is in global variables or local variables or involves subscripting. Some constant values like 0, 1, -1, and some powers of two are encoded compactly and take different amounts of storage and execution time.

    By the way, don't use cut and paste on code in postings without using [ code ] tags as described here:

    attachment.php?attachmentid=78421&d=1297987572
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-05 13:02
    One source of variation might be the relative synchronization between the 2 second delay and the amount of time it takes for the CNT - MT value to exceed TBP. Try taking out the WAITCNT.
  • JoeFLJoeFL Posts: 10
    edited 2012-12-05 13:51
    Hi Mike,

    I did try using my loop code without the waitcnt
    I set up a breakpoint in my debugger at the end of the loop
    I looked at the Time Difference TMD1 variable

    Then manually changed my data_ var, compiled and restarted with breakpoint
    I looked at the Time Difference TMD1 variable

    The code execution time pretty much varied the same times as with the waitcnt in the code

    The variation was from up to 12_000 clock tics, far within the time resolution of the required application

    Its been many moons since I did any assembly programming, but I sorta understand your point
    about division not having a finite execution time.

    Thanks Mike

    PUB Detect_Rate
    
    
      vp.config(string("var:TBP,data_,MT1D"))
      vp.share(@TBP,@MT1D)       'share variables
      
      data_   := 18                     'sets product delivery rate
      TBP  := (3600*80_000) / data_     'TBP Time Between Product in clock tics
                                        ' 80_000_000 tics/sec
    repeat
    
    
      if MT_St == 0                     
         MT := cnt                      'MT Mark Time, 1st time thru loop
         MT_St := 1
      if MT_St 
         MT1 := cnt - MT                'keep looking at MT1
         if MT1 => TBP                  
            MT_St := 0
            MT1D := MT1 - TBP           'This difference in Time seems to vary quite a bit
    
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-05 14:48
    Attached is an archive used with the beta release of SimpleIDE with BSTC (Brad's Spin Tool Command-line) that I used with a Propeller C3 board and a VGA monitor.

    When I run this with the WAITCNT and the following two lines removed, I get a display of 2048 that stays fixed. When I run this with the WAITCNT and the other two lines, I get a display that varies from somewhat under 1000 to around 2000. I think this is due to the WAITCNT which causes a 2 second delay that may finish sometime after CNT has passed the TBP point. I think that accounts for the variation. Because data_ is being incremented every few seconds, I expect the displayed value will drift as TBP changes.
Sign In or Register to comment.