Code efficiency

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Code efficiency

554 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by qili on Tue Sep 14 17:48:52 MST 2010
I am curious as to how efficient the code red compiler (gnu?) is.

I compiled the following code under lpcxpresso 3.5 for 1343.

/*
===============================================================================
 Name        : main.c
 Author      : 
 Version     :
 Copyright   : Copyright (C)
 Description : main definition
===============================================================================
*/

#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif

// TODO: insert other include files here

// TODO: insert other definitions and declarations here
#define LED_PORTLPC_GPIO0->DATA//FIOPIN for Keil
#define LED_DDRLPC_GPIO0->DIR//FIODIR for Keil
#define LED(1<<7)
#define DLY_MS1000//delay time, in ms

volatile unsigned long SysTickCnt=0;      /* SysTick Counter                    */

void SysTick_Handler (void) {           /* SysTick Interrupt Handler (1ms)    */
SysTickCnt++;
}

void Delay (unsigned long tick) {       /* Delay Function                     */
  unsigned long systick;
  //unsigned long tick_cnt = tick;

  systick = SysTickCnt;//systickcnt has the start-up time
  while ((SysTickCnt - systick) < tick);
}

void mcu_init(void) {
SystemInit();//reset mcu
/* Generate interrupt each 1 ms   */
    SysTick_Config(SystemCoreClock/1000 - 1);//=SystemFrequency for Keil

LED_PORT &=~(LED);//clear led
LED_DDR |= (LED);//led as output
}

int main(void) {
unsigned char i;
mcu_init();//reset mcu
while (1) {
for (i=0; i<0xff; i++) {
LED_PORT ^= (LED);
Delay(i<<1);//delay some time
}
for (i=0xff; i>0x00; i--) {
LED_PORT ^= (LED);
Delay(i<<1);//delay some time
}
}
}


the code compiled to about 2.5kb - enabling optimization didn't make much of an impact.

with a certain commercial compiler, I got anywhere between 0.9Kb to 1.1kb (no optimization at all).

has anyone done a more systemic assessment on that front?
0 Kudos
4 Replies

514 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by woodchuck on Thu Sep 16 13:18:17 MST 2010
I can't speak authoritatively about code size optimizations, but I've observed that the code_red/gcc optimizations for speed are very impressive!
0 Kudos

514 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Jesse.Rosenberg on Wed Sep 15 16:22:01 MST 2010
You may also find this application note useful in terms of:
1) Assessing the stack up of code size for a particular application
2) Finding ways to improve your code size

""AN10963 Reducing code size for LPC11XX with LPCXpresso"
http://ics.nxp.com/support/documents/microcontrollers/zip/an10963.zip

Hope that's useful
0 Kudos

514 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Tue Sep 14 23:41:38 MST 2010
When I build your example (and CMSIS) for release, I end up with a code size of 1.7k.

Are you comparing eggs with eggs. Are you building the exact same code (including CMSIS) with both toolchains?

Don't forget that for noddy examples like this, the library code  (startup code, CMSIS, and C runtime) tends to dominate. When you start  adding more of your own code, then your code will dominate over the library code. In our experiments, in terms of code size, GCC is pretty close to 'commercial' - sometimes it is bigger, sometimes it is smaller.
0 Kudos

514 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by domen on Tue Sep 14 23:17:41 MST 2010
Of course that is not all that lands in the binary.

What are your gcc options? Try [font=courier]-Os[/font], [font=courier]-ffunction-sections[/font], maybe [font=courier]-fno-builtin[/font], if you can get away with that. And then the linker option [font=courier]--gc-sections[/font].

Also, analyse resulting code with [font=courier]nm -S --size-sort[/font] and [font=courier]objdump -xd[/font], to get a feel what's using all the FLASH.
0 Kudos