Difference between revisions of "Performance optimization"

From Gamebuino Wiki
Jump to: navigation, search
(Minimum variables size)
(Minimum variables size)
Line 25: Line 25:
 
{|class="wikitable"
 
{|class="wikitable"
 
|+ Arduino variable types
 
|+ Arduino variable types
! Type          || Values         || Size
+
! Type          || Values           || Size
 
|-  
 
|-  
|byte          || 0..255          || 8
+
|byte          || 0 to 255          || 8
 
|-  
 
|-  
|char          || -127..128      ||8
+
|char          || -127 to 128      ||8
 
|-  
 
|-  
|unsigned int  || 0..65,535      ||16
+
|unsigned int  || 0 to 65,535      ||16
 
|-  
 
|-  
|int            || −32,768..32,767 ||16
+
|int            || −32,768 to 32,767 ||16
 
|}
 
|}
  

Revision as of 2013-10-30T18:56:34

Introduction

Gamebuino is a video game console which has limited hardware (8-bits CPU running at 16Mhz and 2KB of RAM...). It's far enough if you want to program Pong or Tetris, but if you plan to do some advanced games with multiplayer and artificial intelligence, you will probably have to optimize your game for it to run fast enough and fit in the chip's memory.

Most of the optimization work is done at high level, by using smart algorithms. But sometimes it's not enough, and to go further you have to get into more system-specific programming to gain speed and memory usage.

General optimization

Premature optimization

Don't optimize something that doesn't need to be. First write a clean code, something easy to read and maintain. Then, only if you actually need performance improvement, benchmark your code to know what is most time consuming and likely to be optimized. Don't try to guess what should be optimized, because you'll be wrong most of the time, and you'll spend time optimizing something that isn't the bottleneck.

Don't be smart

"You should think REALLY, REALLY hard about sacrificing code clarity for the sake of speed or space." westfw on Arduino forum

Other tips & tricks

System specific optimization

Minimum variables size

You should always use the smallest variable type you need. Because Gamebuino runs on a 8-bits microcontroller, 16 and 32 bits variables takes a lot of time and memory, and floating point variables are even worse than integers.

Arduino variable types
Type Values Size
byte 0 to 255 8
char -127 to 128 8
unsigned int 0 to 65,535 16
int −32,768 to 32,767 16

Loops

SPI

I2C / TWI

Clock frequency

Assembly language

Other tips & tricks

References

For further information, you can read the following :