Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7683893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:54:02+00:00 2026-05-31T18:54:02+00:00

Say I’m looping through like 20/30 objects or in any other case where I’m

  • 0

Say I’m looping through like 20/30 objects or in any other case where I’m dealing with smaller numbers, is it a good practice to use short instead of int?

I mean why isn’t this common:

for(short i=0; i<x; i++)
    Method(array[i]);

Is it because the performance gain is too low?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:54:03+00:00Added an answer on May 31, 2026 at 6:54 pm

    “is it a good practice to use short instead of int?”

    First of all, this is a micro optimization that will not achieve the expected results: increase speed or efficiency.

    Second: No, not really, the CLR internally still uses 32 bit integers (Int32) to perform the iteration. Basically it converts short to Int32 for computation purposes during JIT compilation.

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

    If we take the next code:

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

    And disassemble it, you can clearly see at 00000089 inc eax that at machine level an 32 bit register was used for the iterating variable (eax), which is next truncated to 16 bit 0000008a movsx eax,ax so there are no benefits from using a short oppossed to using an int32, actually there might be a slight performance loss due to extra instructions that need to be executed.

    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 
                    Method(array[i]);
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a class named Frog, it looks like: public class Frog {
Say a development team includes (or makes use of) graphic artists who create all
Say I have a SqlAlchemy model something like this: from sqlalchemy.ext.declarative import declarative_base from
say I have input data like so: firstName | lastName | Country Bob |
Say I have a container with various elements inside. In turn, any element could
Say I have two types of objects, Apples and Chainsaws . A user is
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
say i have the 3 non blocking sends like this MPI_Isend (); MPI_Isend ();
Say, the field of Race in my table has values like W,A (white and
Say I have a site like stackoverflow. I need to add a search functionality,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.