Go Down

Topic: code size (Read 1 time) previous topic - next topic

pYro_65

#5
Jan 29, 2013, 04:12 pm Last Edit: Jan 29, 2013, 04:17 pm by pYro_65 Reason: 1
Using this sketch as the bare minimum:
Code: [Select]
void setup() {
 pinMode( 13, OUTPUT );
 digitalWrite( 13, LOW );
}

void loop() {}


I got the values:
Quote
Uno: 866, difference: 250
Mega: 1422, difference: 240
Due: 9628, difference: 40


Although, due to the DUE using direct  port IO, I think it is unfair using this sketch as its bare minimum. Even still the Arduino Uno & Mega are using function calls.

pYro_65

Using this sketch as the blink code:

Code: [Select]

#include <FastDigitalIO.h>

void setup() {  
FastDigitalIO< 13 > p;  
p.mode( OUTPUT );
}

// the loop routine runs over and over again forever:
void loop() {
 FastDigitalIO< 13 > p;  
 p.write( HIGH );
 delay(1000);               // wait for a second
 p.write( LOW );
 delay(1000);               // wait for a second
}


I get values like ( taken off real bare minimum, not the one I posted above ):

Quote
UNO direct IO: 706, difference 240
Mega direct IO: 898, difference 232

pagoda


Using this sketch as the blink code:

Code: [Select]

#include <FastDigitalIO.h>

void setup() {  
FastDigitalIO< 13 > p;  
p.mode( OUTPUT );
}

// the loop routine runs over and over again forever:
void loop() {
 FastDigitalIO< 13 > p;  
 p.write( HIGH );
 delay(1000);               // wait for a second
 p.write( LOW );
 delay(1000);               // wait for a second
}


I get values like ( taken off real bare minimum, not the one I posted above ):

Quote
UNO direct IO: 706, difference 240
Mega direct IO: 898, difference 232



That sounds better.. if the basic initialisation code is a little bulky, BUT program code is then fairly efficient ,there is less to worry about than I thought. I have some code that ends up >50Kb on a Mega at the moment. Some of it won't easily transplant until libraries are sorted out, but it looks hopeful  :)

pYro_65

No matter how effiecient the Due is, its instructions have a larger footprint over the AVR, here is a blink sketch I whipped up.

It compiles to 208 bytes on the Uno. Using a timer it could well be less.

Code: [Select]

#include <FastDigitalIO.h>

int main(void) __attribute__ ((naked)) __attribute__ ((section (".init9")));
inline void Wait( void ) { __asm__ __volatile__( "nop" ); }

__attribute__( ( noinline ) ) void DelayIt( void ){
  uint32_t u_Count = 0x001EFFFF;
  while( --u_Count ){
    Wait();     
  }
}

int main() {
  FastDigitalIO< 13 > p; 
  p.mode( OUTPUT );
 
  while( true ){
    p.write( HIGH );
    DelayIt();
    p.write( LOW );
    DelayIt();
  }
  return 0;
}


I have included fat16lib's code if you are interested in running my examples.

stimmer

Just to add to the confusion, here's blink with an unrolled loop - 2000*digitalWrite and delay.
Code: [Select]
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

#define do10(x) x x x x x x x x x x
#define do50(x) do10(x) do10(x) do10(x) do10(x) do10(x)
#define do250(x) do50(x) do50(x) do50(x) do50(x) do50(x)
#define do1000(x) do250(x) do250(x) do250(x) do250(x)

// the loop routine runs over and over again forever:
void loop() {
do1000(
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
)
}

Code: [Select]
code size         mega    due
normal blink      1662    9740
unrolled blink    44866   41716


Due beats Mega by 3k :D

This bears out my general experience with the Due - the larger code sizes are because it has a more comprehensive runtime. Ignoring the overhead, code for code the Due seems to be slightly better than the Mega.

Go Up