Why can using unsigned short be slower than using int?

Viewed 184

On this webpage, the comments on unsigned short state:

Used to reduce memory usage (although the resulting executable may be larger and probably slower as compared to using int

Why is this?

2 Answers

I think the wording "probably slower" is too hard.

A theoretical fact is:

Calculations are done with at least int size, e.g.

short a = 5;
short b = 10;
short c = a + b;

This code snippet contains 3 implicit conversions. a and b are converted to int and added. The result is converted back from int to short. You can't avoid it. Arithmetic in C++ uses at least int size. It's called integer promotion.

In practice most conversions will be avoided by the compiler. And the optimizer will also remove many conversions.

Oftentimes, the short will be modified into an int when passed into a function. This will require some additional generated code to do that. So even if you define a short, the function will take it as an int.

Related