Profiling Code: More Than One Way to Skin A Cat

skinned cat
I found an excellent profiling library for the Arduino posted by the seemingly inexhaustible Arduino Forum poster Nick Gammon. You can find Nick’s code here.

Here is the program code from my cycle counting post with Nick’s profiling library inserted (minimal intrusion):

#include <ProfileTimer.h>

void setup() {
  Serial.begin(115200);
}

void loop() {
  asm volatile (
       "sbi %0, %1 \n\t" //LED on
	   :: "I" (_SFR_IO_ADDR(PORTB)), "I" (PORTB5)
  );

  {
    ProfileTimer t ("inline delay");
    asm volatile (
      "    ldi r20, 32  \n\t"
      "1:  ldi r21, 200 \n\t"
      "2:  ldi r22, 250 \n\t"
      "3:  nop          \n\t"
      "    nop          \n\t"
      "    dec r22      \n\t"
      "    brne 3b      \n\t"
      "    dec r21      \n\t"
      "    brne 2b      \n\t"
      "    dec r20      \n\t"
      "    brne 1b      \n\t"
      ::: "r20", "r21", "r22"
    );
  }

  asm volatile (
    "cbi %0, %1 \n\t" //LED off
    "    ldi r20, 32  \n\t"
    "1:  ldi r21, 200 \n\t"
    "2:  ldi r22, 250 \n\t"
    "3:  nop          \n\t"
    "    nop          \n\t"
    "    dec r22      \n\t"
    "    brne 3b      \n\t"
    "    dec r21      \n\t"
    "    brne 2b      \n\t"
    "    dec r20      \n\t"
    "    brne 1b      \n\t"
    :: "I" (_SFR_IO_ADDR(PORTB)), "I" (PORTB5)
    : "r20", "r21", "r22"
  );
}

Here is the output running Nick Gammon’s Profile Timer:

Start     : inline delay
Time taken: inline delay = 504196 uS.
Start     : inline delay
Time taken: inline delay = 504200 uS.
Start     : inline delay
Time taken: inline delay = 504196 uS.
Start     : inline delay
Time taken: inline delay = 504196 uS.
Start     : inline delay
Time taken: inline delay = 504200 uS.
Start     : inline delay
Time taken: inline delay = 504204 uS.
Start     : inline delay
Time taken: inline delay = 504196 uS.
Start     : inline delay
Time taken: inline delay = 504200 uS.
Start     : inline delay
Time taken: inline delay = 504196 uS.
Start     : inline delay
Time taken: inline delay = 504196 uS.
...
Advertisements

About Jim Eli

µC experimenter
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s