which to prefer: int8, int16, int32 ?

Joegi_Lov
Posts: 9
Joined: Tue Feb 27, 2018 7:07 pm

which to prefer: int8, int16, int32 ?

Postby Joegi_Lov » Sun Apr 08, 2018 6:59 pm

Hi, on x86 32/64 bit CPUs 32bit variables often are processed faster than 16 or 8 bit variables. What about the ESP32? Which type is fastest for calculations (in average): in8_t, int16_t or int32_t?
Thanks for the answers in advance!

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: which to prefer: int8, int16, int32 ?

Postby kolban » Sun Apr 08, 2018 7:16 pm

The instruction set of the Xtensa processors are 32 bits in size. Most memory retrievals work best on a 32bit boundary. However, that said, I can't immediately see a reason for performance to suffer if one used an integer representation less than the native representation.

For me, my goto for a variable is a 32bit unsigned.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Joegi_Lov
Posts: 9
Joined: Tue Feb 27, 2018 7:07 pm

Re: which to prefer: int8, int16, int32 ?

Postby Joegi_Lov » Sun Apr 08, 2018 7:32 pm

Thanks!

Archibald
Posts: 110
Joined: Mon Mar 05, 2018 12:44 am

Re: which to prefer: int8, int16, int32 ?

Postby Archibald » Mon Apr 09, 2018 12:00 pm

I think it's necessary to time how long calculations take rather than make assumptions.

I declared an integer array of 10,000 elements. After initialising the array with some values, within a loop I added 15 to every element. Allowing for printing micros() taking 14μs, calculation times (including the loop processing) were:
int8_t array: 608μs
int16_t array: 268μs
int32_t array: 268μs

Explain that!

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: which to prefer: int8, int16, int32 ?

Postby kolban » Mon Apr 09, 2018 12:44 pm

We can compile the source file with the -S flag and examine the generated assembly statements. From that, we may be able to see that for single bytes, additional instructions are required. If that is the case, then that would explain the additional time as the total number of instructions executed to perform a task (the path length) is directly proportional to the time taken to execute the task as a whole.

For example, if writing to an address in storage is at the 32bit level, then to write a new value into ONE of those bytes would require:

read original value
mask and shift byte into 32 bit word
boolean AND out target bits
boolean OR in the new value
write the new value

while writing a 32 bit value would be

write the new value

Again, just assumptions.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Joegi_Lov
Posts: 9
Joined: Tue Feb 27, 2018 7:07 pm

Re: which to prefer: int8, int16, int32 ?

Postby Joegi_Lov » Mon Apr 09, 2018 5:03 pm

I forgot that most compilers are able to compile to asm. I will use this in future to see how things are compiled! - Thanks!

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: which to prefer: int8, int16, int32 ?

Postby tele_player » Mon Apr 09, 2018 5:26 pm

Keep in mind, the steps Kolban mentioned above might be done in CPU microcode. Same effect - some types of access are slower than others.

Who is online

Users browsing this forum: No registered users and 44 guests