Difference between revisions of "Performance optimization"
m (→General optimization) |
|||
Line 5: | Line 5: | ||
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. | 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. | ||
− | + | == 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 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 == | |
<blockquote>"You should think REALLY, REALLY hard about sacrificing code clarity for the sake of speed or space." [http://forum.arduino.cc//index.php?PHPSESSID=3fa28strc8ot5opa47hq14ce71&topic=44924.msg325401#msg325401 westfw on Arduino forum]</blockquote> | <blockquote>"You should think REALLY, REALLY hard about sacrificing code clarity for the sake of speed or space." [http://forum.arduino.cc//index.php?PHPSESSID=3fa28strc8ot5opa47hq14ce71&topic=44924.msg325401#msg325401 westfw on Arduino forum]</blockquote> | ||
− | + | == Variables == | |
− | |||
− | |||
− | + | === data types === | |
'''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. | '''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. | ||
Line 46: | Line 42: | ||
For more information about the data type refer to [http://arduino.cc/en/Reference/HomePage Arduino Reference]. | For more information about the data type refer to [http://arduino.cc/en/Reference/HomePage Arduino Reference]. | ||
− | + | === #define === | |
You can use [http://arduino.cc/en/Reference/Define #define] for constant variables that will never change, like pin numbers. This way it won't take any space in the chip's memory. | You can use [http://arduino.cc/en/Reference/Define #define] for constant variables that will never change, like pin numbers. This way it won't take any space in the chip's memory. | ||
Line 53: | Line 49: | ||
<pre>#define ledPin 13</pre> | <pre>#define ledPin 13</pre> | ||
− | + | === progmem === | |
For constant large arrays or bitmaps you can use progmem to avoid loading them in RAM. [http://arduino.cc/en/Reference/PROGMEM Arduino article] explains that, for more info read [http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38003&start=all&postdays=0&postorder=asc a complete tutorial]. | For constant large arrays or bitmaps you can use progmem to avoid loading them in RAM. [http://arduino.cc/en/Reference/PROGMEM Arduino article] explains that, for more info read [http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38003&start=all&postdays=0&postorder=asc a complete tutorial]. | ||
− | + | == Operations & Math == | |
− | + | === Arithmetic operators === | |
Addition is the fastest, multiplication is slower, and division is even slower. | Addition is the fastest, multiplication is slower, and division is even slower. | ||
Line 65: | Line 61: | ||
To multiply or divide by a power of 2 you can use bitwise operators << and >> but remember, [[#Don't be smart|don't be smart]]. | To multiply or divide by a power of 2 you can use bitwise operators << and >> but remember, [[#Don't be smart|don't be smart]]. | ||
− | + | === Math === | |
Avoid using sin() cos() and these kind of operations, because they are very slow and take a lot of memory. Moreover, they manipulate floating point variables, which are also time and memory consuming. | Avoid using sin() cos() and these kind of operations, because they are very slow and take a lot of memory. Moreover, they manipulate floating point variables, which are also time and memory consuming. | ||
− | + | == Loops == | |
− | + | == Communication == | |
− | + | === Serial === | |
− | + | === SPI === | |
− | + | === I2C / TWI === | |
− | + | == Clock frequency == | |
− | + | == Arduino vs native AVR == | |
− | + | == Assembly language == | |
− | + | == Other tips & tricks == | |
== Further reading == | == Further reading == |
Revision as of 2013-10-30T21:17:53
Contents
Introduction
Gamebuino is a video game console which has limited hardware (8-bits CPU running at 16Mhz and 2KB of RAM...). It's 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.
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
Variables
data types
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.
Most of the time you can re-arrange math to avoid the need of floating point. For example, the battery voltage is stored in mV instead of V, so we can use a unsigned int instead of a floating point.
Type | Min | Max | Size | Perf |
---|---|---|---|---|
byte | 0 | 255 | 8 | :) |
char | -128 | 127 | 8 | :) |
unsigned int | 0 | 65 535 | 16 | :/ |
int | −32 768 | 32 767 | 16 | :/ |
unsigned long | 0 | 4,294,967,295 | 32 | :( |
long | -2,147,483,648 | 2,147,483,647 | 32 | :( |
float / double | -3.4028235E+38 | 3.4028235E+38 | 32 | :'( |
For more information about the data type refer to Arduino Reference.
#define
You can use #define for constant variables that will never change, like pin numbers. This way it won't take any space in the chip's memory.
Here is an example :
#define ledPin 13
progmem
For constant large arrays or bitmaps you can use progmem to avoid loading them in RAM. Arduino article explains that, for more info read a complete tutorial.
Operations & Math
Arithmetic operators
Addition is the fastest, multiplication is slower, and division is even slower.
To multiply or divide by a power of 2 you can use bitwise operators << and >> but remember, don't be smart.
Math
Avoid using sin() cos() and these kind of operations, because they are very slow and take a lot of memory. Moreover, they manipulate floating point variables, which are also time and memory consuming.