0
Offline
Full Member
Karma: 0
Posts: 201
Arduino rocks
|
 |
« on: May 17, 2012, 08:35:42 am » |
Good afternoon
I would like to optimize Arduino RAM utilization. I use 'Int' variables instead 'Byte' because it seems to me more practical to use in comparisons, conversions...
I would like to know if I'm improving something using 'uint8_t' instead 'Int'... because some variables that I'm using are in 0-255 range.
Please let me know Thanks on advance Best regards Pedro Ferrer
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19367
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: May 17, 2012, 08:37:47 am » |
I would like to know if I'm improving something using 'uint8_t' instead 'Int'.. Yes, quicker and consuming less program and RAM memory.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36434
Seattle, WA USA
|
 |
« Reply #2 on: May 17, 2012, 08:53:03 am » |
I use 'Int' variables instead 'Byte' because it seems to me more practical to use in comparisons, conversions. How? The type of a variable matters little when constructing an if statement, for instance. When converting from one type to another to another, the types matter, but converting from byte to float is exactly the same as converting from int to float. This statement needs some additional explanation, I think.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 201
Arduino rocks
|
 |
« Reply #3 on: May 17, 2012, 09:02:56 am » |
Hello
Thanks by your prompt answers.
How much I'll improving RAM on each 'uint8_t' instead 'Int' ?
(...)
PaulS Eg: using ITOA... Integer to Ascii... is some of the reason that's why I'm using 'Int' instead 'Byte'
Thanks once again Best regards Pedro Ferrer
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19367
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: May 17, 2012, 09:13:21 am » |
The natural word size of the AVR's RAM datapaths is 8 bits, so a "byte" type (8 bits) will be fetched into a CPU register more quickly than an "int" type (16 bits), and an "int" will require more program memory to hold the extra instructions to fetch and manipulate it.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36434
Seattle, WA USA
|
 |
« Reply #5 on: May 17, 2012, 09:14:18 am » |
using ITOA... Integer to Ascii... is some of the reason that's why I'm using 'Int' instead 'Byte' You may need to learn about casting, then. Typically, a value will be implicitly cast to a larger type, but must be explicitly cast to a smaller type. So, a byte (or uint8_t) will be implicitly cast to an int for itoa() to use, but you might need to explicitly cast the int returned by atoi() to a byte (or uint8_t). How much I'll improving RAM on each 'uint8_t' instead 'Int' ? 50%. A uint8_t is one byte. An int is two bytes.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 201
Arduino rocks
|
 |
« Reply #6 on: May 17, 2012, 10:28:46 am » |
Hello
Thanks to you, I've changed (not all yet) some 'Int' to 'uint8_t'... result... my sketch length on Mega 2560: Before was ~133000 bytes Now is ~113000 bytes!!!
Amazing! Finishing the changes, my next step will be apply 'Progmem' to manipulate my string arrays to LCD.
Thanks once again by your help! Best regards Pedro Ferrer
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #7 on: May 17, 2012, 12:18:02 pm » |
Thanks to you, I've changed (not all yet) some 'Int' to 'uint8_t'... result... my sketch length on Mega 2560: Before was ~133000 bytes Now is ~113000 bytes!!!
Just to clarify, the RAM usage does not directly affect sketch length. As an example: byte foo [1000]; // takes 1000 bytes of RAM int bar [1000]; // takes 2000 bytes of RAM Whilst the compiler generates more code to access "bar" than "foo" it will be a few bytes more (of sketch size) not 1000 bytes more.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 201
Arduino rocks
|
 |
« Reply #8 on: May 18, 2012, 02:19:02 am » |
Good morning
Hum... Something happened, because beside change 'Int' to 'uint8_t', I've applied some 'const uint8_t' instead 'Int'... and get ~20000 bytes less...
Best regards Pedro Ferrer
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #9 on: May 18, 2012, 02:27:43 am » |
Yes, no doubt. I would like to optimize Arduino RAM utilization.
... my sketch length on Mega 2560: Before was ~133000 bytes Now is ~113000 bytes!!! The sketch length is nothing to do with RAM. It is program memory size, which is a totally different part of the processor.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 201
Arduino rocks
|
 |
« Reply #10 on: May 18, 2012, 02:52:03 am » |
Hello
Ok... more details...
After replace variables 'Int' 616 times by 'uint8_t' my sketch reduced from 132146 bytes to 113890 bytes using arduino.exe compiler!
This is what I get.
Please let me know Thanks on advance Best regards Pedro Ferrer
|
|
|
|
|
Logged
|
|
|
|
|
North Queensland, Australia
Offline
Edison Member
Karma: 35
Posts: 1279
|
 |
« Reply #11 on: May 18, 2012, 05:47:17 am » |
Did any of those 616 numbers ever need to count above 255 or are they ever negative, if so you may run into problems.
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 51
Posts: 2175
|
 |
« Reply #12 on: May 18, 2012, 09:37:48 am » |
After replace variables 'Int' 616 times by 'uint8_t' my sketch reduced from 132146 bytes to 113890 bytes using arduino.exe compiler!
This is what I get.
Please let me know
Let you know what? You didn't ask a question.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 201
Arduino rocks
|
 |
« Reply #13 on: May 18, 2012, 11:41:17 am » |
Hello Let you know what? You didn't ask a question.
Please let me know... because I don't understand why the sketch was reduced in so many bytes... Thanks on advance Best regards Pedro Ferrer
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 101
Posts: 9551
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #14 on: May 18, 2012, 12:05:31 pm » |
read this book - http://letrongngoc.tech.officelive.com/Documents/TheCProgrammingLanguageSecondEdition.pdf - especially the part about sizeof, better read the whole book. It are the basics of programming explained by the creators of C and UNIX. It will take a while to go through the book but you will learn so much from it ... If you really want to learn why the executable got smaller you need to learn about compiler building, assembly and that kind of stuff. Not for the faint of heart With the tool objdump.exe you can see the assembly language generated by the arduino compiler and see what has happened. [in short, changing datatypes from int16 to int8 size gave the compiler the chance to use other assembly instructons to do the code] If questions remain, please feel free to ask them.
|
|
|
|
|
Logged
|
|
|
|
|
|