When to use the short one?

Let's say I am looping through 20/30 objects or whatever, when I am dealing with smaller numbers, is it helpful to use short instead of int?

I mean, why is it not so common:

for(short i=0; i<x; i++)


Is it because the gain is too low?



source to share

4 answers

"is it good practice to use short instead of int?"

First of all, this is micro-optimization, which will not allow you to achieve the expected results: increase speed or efficiency.

Second: No, actually, the CLR internally still uses 32-bit integers (Int32) to iterate. It basically converts short to Int32 for JIT compile-time computation purposes.

Third: Array indices are Int32 and the short iteration variable is automatically converted to int32 when used as an array indexer.

If we take the following code:

    var array = new object[32];
    var x = array.Length;
    for (short i = 0; i < x; i++)


And by disassembling it, you can clearly see at 00000089 inc eax

the machine level for the variable iteration (eax) a 32-bit register was used, which is then truncated to 16 bits 0000008a movsx eax,ax

, so there is no benefit from using the short opposite of using int32, in fact there may be a little loss of performance due to additional instructions that need to be followed.

00000042  nop 
            var array = new object[32];
00000043  mov         ecx,64B41812h 
00000048  mov         edx,20h 
0000004d  call        FFBC01A4 
00000052  mov         dword ptr [ebp-50h],eax 
00000055  mov         eax,dword ptr [ebp-50h] 
00000058  mov         dword ptr [ebp-40h],eax 
            var x = array.Length;
0000005b  mov         eax,dword ptr [ebp-40h] 
0000005e  mov         eax,dword ptr [eax+4] 
00000061  mov         dword ptr [ebp-44h],eax 
            for (short i = 0; i < x; i++)
00000064  xor         edx,edx 
00000066  mov         dword ptr [ebp-48h],edx 
00000069  nop 
0000006a  jmp         00000090 
0000006c  mov         eax,dword ptr [ebp-48h] 
0000006f  mov         edx,dword ptr [ebp-40h] 
00000072  cmp         eax,dword ptr [edx+4] 
00000075  jb          0000007C 
00000077  call        657A28F6 
0000007c  mov         ecx,dword ptr [edx+eax*4+0Ch] 
00000080  call        FFD9A708 
00000085  nop 
            for (short i = 0; i < x; i++)
00000086  mov         eax,dword ptr [ebp-48h] 
00000089  inc         eax 
0000008a  movsx       eax,ax 
0000008d  mov         dword ptr [ebp-48h],eax 
00000090  mov         eax,dword ptr [ebp-48h] 
00000093  cmp         eax,dword ptr [ebp-44h] 
00000096  setl        al 
00000099  movzx       eax,al 
0000009c  mov         dword ptr [ebp-4Ch],eax 
0000009f  cmp         dword ptr [ebp-4Ch],0 
000000a3  jne         0000006C




Yes, the performance difference is negligible. However, the short one uses 16 bits instead of 32 for an int, so it's entirely possible that you can use the short one if you're handling small enough numbers.



In general, using numbers that match the processor word size is relatively faster than unlocking numbers. On the other hand, shrinking uses less memory space than int. If you have limited memory space, using the short option may be an alternative; but personally, I have never encountered such a situation when writing C # applications.



An int

uses 32 bits of memory, a short

uses 16 bits and byte

uses 8 bits. If you are only looping objects at 20/30 and you are concerned about memory usage, use byte


Maintaining memory usage at this level is rarely required with today's computers, although you could argue that usage int

everywhere is just lazy. Personally, I try to always use the appropriate type that uses the least memory.




All Articles