Page 1 of 1

which to prefer: int8, int16, int32 ?

Posted: Sun Apr 08, 2018 6:59 pm
by Joegi_Lov
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!

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

Posted: Sun Apr 08, 2018 7:16 pm
by kolban
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.

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

Posted: Sun Apr 08, 2018 7:32 pm
by Joegi_Lov
Thanks!

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

Posted: Mon Apr 09, 2018 12:00 pm
by Archibald
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!

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

Posted: Mon Apr 09, 2018 12:44 pm
by kolban
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.

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

Posted: Mon Apr 09, 2018 5:03 pm
by Joegi_Lov
I forgot that most compilers are able to compile to asm. I will use this in future to see how things are compiled! - Thanks!

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

Posted: Mon Apr 09, 2018 5:26 pm
by tele_player
Keep in mind, the steps Kolban mentioned above might be done in CPU microcode. Same effect - some types of access are slower than others.