One potential improvement over int as a loop counter is unsigned int (or std::size_t where applicable) if the loop index is never negative. Using short instead of int has nothing to do with most compilers, here are the ones I have.
the code:
volatile int n; int main() { for(short j = 0; j < 50; j++)
g ++ 4.5.2 -march = native -O3 on x86_64 linux
// using short j // using int j .L2: .L2: movl %eax, n(%rip) movl %eax, n(%rip) incl %eax incl %eax cmpl $50, %eax cmpl $50, %eax jne .L2 jne .L2
clang ++ 2.9 -march = native -O3 on x86_64 linux
// using short j // using int j .LBB0_1: .LBB0_1: movl %eax, n(%rip) movl %eax, n(%rip) incl %eax incl %eax cmpl $50, %eax cmpl $50, %eax jne .LBB0_1 jne .LBB0_1
Intel C ++ 11.1 -fast on x86_64 linux
// using short j // using int j ..B1.2: ..B1.2: movl %eax, n(%rip) movl %eax, n(%rip) incl %edx incl %eax movswq %dx, %rax cmpl $50, %eax cmpl $50, %eax jl ..B1.2 jl ..B1.2
Sun C ++ 5.8 -xO5 on sparc
// using short j // using int j .L900000105: .L900000105: st %o4,[%o5+%lo(n)] st %o4,[%o5+%lo(n)] add %o4,1,%o4 add %o4,1,%o4 cmp %o4,49 cmp %o4,49 ble,pt %icc,.L900000105 ble,pt %icc,.L900000105
So, of the four compilers that I have, only one even had any difference in the result, and in fact it used fewer bytes in the int case.