Shop OBEX P1 Docs P2 Docs Learn Events
How to Determine Elapsed Time of a Loop — Parallax Forums

How to Determine Elapsed Time of a Loop

Hello,

Is there a way to determine the elapsed time of a loop in Propeller C?

Thank you.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2018-10-28 21:52
    Usually you save a copy of CNT just before the loop starts, then subtract that from CNT just after the loop ends. The difference is the number of elapsed system clocks. You need to #include <propeller.h> in your program. For example:
    #include <propeller.h>
    
    int tryTiming(void) {
      int startTime;
      int i, j, k;
      startTime = CNT;  // remember starting time
      for (i=1;i<=100;i++)
        for (j=1;j<=100;j++) k = i * 12345 / j;  // multiply and divide
      return CNT - startTime;  // return time difference in system clocks
    
    We have 10,000 multiplies and divides plus assorted other operations. The system counter rolls over after 2^32 clock cycles so you need to keep the total loop time under roughly 53 seconds (2^32 / 80000000) using the normal 80MHz system clock. In this case, the values are signed, so the maximum loop time is 26 seconds.
  • Thanks for your reply, Mike. Using your code, I've done the following:
    #include propeller.h
    float startTime;
    float delta;
    // Do Prediction Step, note: filter is not using accel measurements at this point, only gyro, so it drifts
        startTime = CNT; // Remember starting time
        int N = 3;
        for(int i = 0; i < N; i++){
    
        ////////////////////
        // State derivatives
        float f[2] = {
          {gy+gx*sin(phi)*tan(theta)+gz*cos(phi)*tan(theta)},
          {gx*cos(phi)-gz*sin(phi)}
        };  
    
        //////////////////////
        // Calculate xhat
        // xhat = xhat + (Ts/N)*f
        xhat[0]=xhat[0]+(delta/N)*f[0];
        xhat[1]=xhat[1]+(delta/N)*f[1];
        phi=xhat[0];
        theta=xhat[1];
    
        print("%f  delta = %f\n",theta*180/PI, delta);
      } 
        delta = (CNT-startTime)/80000000;
    

    Have I implemented this correctly by dividing by 80,000,000 to get the elapsed time in seconds?

    David
  • Currently delta is uninitialised, could give you errors when you get to:
    // Calculate xhat
        // xhat = xhat + (Ts/N)*f
        xhat[0]=xhat[0]+(delta/N)*f[0];
        xhat[1]=xhat[1]+(delta/N)*f[1];
    
  • Yes, you divide by 80000000 to get seconds assuming the standard system clock of 80MHz.
  • 78rpm,

    Thanks. I have now initialized delta to an estimated value. Before it was assuming delta is zero.

    Mike,

    Thanks so much for your help. I have not changed anything with the clock so I'm assuming it is still set to 80 MHz.

    David
  • Unless you change the crystal on the board from 5MHz to something else, the system clock will normally be set to 80MHz (there’s a multiplier of x16 set by a constant (_clkmode) compiled as part of your program with a value of “pll16x”). The crystal frequency is specified by another constant (_xinfreq).
  • If you have a scope then a common way is to insert test pulses into your code so that when it enters a routine it may take a pin high and when it leaves it goes low again. That way you can see how often, how regular, and how long it is running for.
Sign In or Register to comment.