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!
which to prefer: int8, int16, int32 ?
Re: which to prefer: int8, int16, int32 ?
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.
For me, my goto for a variable is a 32bit unsigned.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: which to prefer: int8, int16, int32 ?
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!
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 ?
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.
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
Re: which to prefer: int8, int16, int32 ?
I forgot that most compilers are able to compile to asm. I will use this in future to see how things are compiled! - Thanks!
-
- Posts: 90
- Joined: Sun Jul 02, 2017 3:38 am
Re: which to prefer: int8, int16, int32 ?
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