Pages: [1] 2   Go Down
Author Topic: 'uint8_t' instead 'Int'  (Read 1871 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24327
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46063
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24327
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46063
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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).

Quote
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 Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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 Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, no doubt.

Quote
I would like to optimize Arduino RAM utilization.

...

Quote
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 Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 53
Posts: 1785
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 0
Posts: 205
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 169
Posts: 12448
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 smiley
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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1] 2   Go Up
Jump to: