Here I was just writing some code that dealt with an integer value of -24/+24, and I made my method return an int… And I thought to myself– should I really be using a short in this case? I know it might have mattered back in the day when memory for something was 48k– but in today’s modern world does it really matter?
Is it ok to just be “int happy”, even when I know my numbers are going to be very small?
All ARM CPUs have 32-bit integer registers and at least a 32-bit wide L1 bus, so using a
shortwill give absolutely no advantage [1], and may in some cases be detrimental to performance.Leave the variable as an
intand be safe in the knowledge that you’ll get an optimum register width pretty much wherever you run the code.[1] The exception to this rule being when using the NEON unit – in which case a 16-bit operation offers more parallelism than a 32-bit operation.