Hello,
when I was looking for a way how to convert char into numeric value for std::cout I found some old discussion with this statement:
Operations on unsignedint are typically faster than on unsignedchar, because your processor cannot fetch single bytes but has to get at least a multiple of 4 and mask out the other 3 bytes.
Is this true? By using smaller (in bytes) variable I actually slow down my program? I always thought that it is a good practice if I use the smallest variable which will do the work.
No other signed integer type can be faster than int.
No other unsigned integer type can be faster than unsigned int.
Plain ints have the natural size suggested by the architecture of the execution environment; the other signed integer types are provided to meet special needs. - IS
and
each signed integer type has the same object representation as its corresponding unsigned integer type - IS
Therefore, integral promotions are automatically applied.
1 2 3 4 5 6 7
char c = 'A' ;
auto c1 = +c ; // type of c1 is 'int'
auto c2 = c - c ; // type of c2 is 'int' : evaluate the result of int(c) - int(c)
short s = 0 ;
auto s1 = +s ; // type of s1 is 'int'
auto s2 = s + s ; // type of s2 is 'int' : evaluate the result of int(s) + int(s)
Operations on objects of type int may be faster:
1 2 3 4 5
int a = 7, b = 8 ;
int i = a + b ;
char c = 7, d = 8 ;
int j = c + d ; // as if: const int temp1 = c ; const int temp2 = d ; int j = temp1 + temp2 ;