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

  • Home
  • SEARCH
  • 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 6870017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:37:50+00:00 2026-05-27T03:37:50+00:00

I’ve noticed a performance hit of iterating over a primitive collection (T[]) that has

  • 0

I’ve noticed a performance hit of iterating over a primitive collection (T[]) that has been cast to a generic interface collection (IList or IEnumberable).

For example:

    private static int Sum(int[] array)
    {
        int sum = 0;

        foreach (int i in array)
            sum += i;

        return sum;
    }

The above code executes significantly faster than the code below, where the parameter is changed to type IList (or IEnumerable):

    private static int Sum(IList<int> array)
    {
        int sum = 0;

        foreach (int i in array)
            sum += i;

        return sum;
    }

The performance hit still occurs if the object passed is a primitive array, and if I try changing the loop to a for loop instead of a foreach loop.

I can get around the performance hit by coding it like such:

    private static int Sum(IList<int> array)
    {
        int sum = 0;

        if( array is int[] )
            foreach (int i in (int[])array)
                sum += i;
        else
            foreach (int i in array)
                sum += i;

        return sum;
    }

Is there a more elegant way of solving this issue? Thank you for your time.

Edit: My benchmark code:

    static void Main(string[] args)
    {
        int[] values = Enumerable.Range(0, 10000000).ToArray<int>();
        Stopwatch sw = new Stopwatch();

        sw.Start();
        Sum(values);
        //Sum((IList<int>)values);
        sw.Stop();

        Console.WriteLine("Elasped: {0} ms", sw.ElapsedMilliseconds);
        Console.Read();
    }
  • 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-27T03:37:51+00:00Added an answer on May 27, 2026 at 3:37 am

    Your best bet is to create overload for Sum with int[] as argument if this method is performance-critical. CLR’s JIT can detect foreach-style iteration over array and can skip range checking and address each element directly. Each iteration of loop takes 3-5 instructions on x86, with only one memory lookup.

    When using IList, JIT does not have knowledge about underlying collection’s structure and ends up using IEnumerator<int>. Each iteration of loop uses two interface invocation – one for Current, one for MoveNext (2-3 memory lookups and a call for each of those). This most likely ends up with ~20 quite expensive instructions and there is not much you can do about it.

    Edit: If you are curious about actual machine code emitted by JIT from Release build without debugger attached, here it is.

    Array version

                int s = 0;
    00000000  push        ebp  
    00000001  mov         ebp,esp 
    00000003  push        edi  
    00000004  push        esi  
    00000005  xor         esi,esi 
                foreach (int i in arg)
    00000007  xor         edx,edx 
    00000009  mov         edi,dword ptr [ecx+4] 
    0000000c  test        edi,edi 
    0000000e  jle         0000001B 
    00000010  mov         eax,dword ptr [ecx+edx*4+8] 
                    s += i;
    00000014  add         esi,eax 
    00000016  inc         edx  
                foreach (int i in arg)
    00000017  cmp         edi,edx 
    00000019  jg          00000010 
    

    IEnumerable version

                int s = 0;
    00000000  push        ebp  
    00000001  mov         ebp,esp 
    00000003  push        edi  
    00000004  push        esi  
    00000005  push        ebx  
    00000006  sub         esp,1Ch 
    00000009  mov         esi,ecx 
    0000000b  lea         edi,[ebp-28h] 
    0000000e  mov         ecx,6 
    00000013  xor         eax,eax 
    00000015  rep stos    dword ptr es:[edi] 
    00000017  mov         ecx,esi 
    00000019  xor         eax,eax 
    0000001b  mov         dword ptr [ebp-18h],eax 
    0000001e  xor         edx,edx 
    00000020  mov         dword ptr [ebp-24h],edx 
                foreach (int i in arg)
    00000023  call        dword ptr ds:[009E0010h] 
    00000029  mov         dword ptr [ebp-28h],eax 
    0000002c  mov         ecx,dword ptr [ebp-28h] 
    0000002f  call        dword ptr ds:[009E0090h] 
    00000035  test        eax,eax 
    00000037  je          00000052 
    00000039  mov         ecx,dword ptr [ebp-28h] 
    0000003c  call        dword ptr ds:[009E0110h] 
                    s += i;
    00000042  add         dword ptr [ebp-24h],eax 
                foreach (int i in arg)
    00000045  mov         ecx,dword ptr [ebp-28h] 
    00000048  call        dword ptr ds:[009E0090h] 
    0000004e  test        eax,eax 
    00000050  jne         00000039 
    00000052  mov         dword ptr [ebp-1Ch],0 
    00000059  mov         dword ptr [ebp-18h],0FCh 
    00000060  push        0F403BCh 
    00000065  jmp         00000067 
    00000067  cmp         dword ptr [ebp-28h],0 
    0000006b  je          00000076 
    0000006d  mov         ecx,dword ptr [ebp-28h] 
    00000070  call        dword ptr ds:[009E0190h] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.