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 8994929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:31:59+00:00 2026-06-15T23:31:59+00:00

I’ve tested List<string> vs IEnumerable<string> iterations with for and foreach loops , is it

  • 0

I’ve tested List<string> vs IEnumerable<string>
iterations with for and foreach loops , is it possible that the List is much faster ?

these are 2 of few links I could find that are publicly stating that performance is better iterating IEnumerable over List.

Link1
Link2

my tests was loading 10K lines from a text file that holds a list of URLs.

I’ve first loaded it in to a List , then copied List to an IEnumerable

List<string> StrByLst = ...method to load records from the file .
IEnumerable StrsByIE =  StrByLst;

so each has 10k items Type <string>

looping on each collection for 100 times , meaning 100K iterations, resulted with

List<string> is faster by amazing 50 x than the IEnumerable<string>

is that predictable ?

  • update

this is the code that is doing the tests

string WorkDirtPath = HostingEnvironment.ApplicationPhysicalPath;
    string fileName = "tst.txt";
    string fileToLoad = Path.Combine(WorkDirtPath, fileName);
    List<string> ListfromStream = new List<string>();
    ListfromStream =  PopulateListStrwithAnyFile(fileToLoad) ;
    IEnumerable<string> IEnumFromStream = ListfromStream ;

    string trslt = "";
    Stopwatch SwFr = new Stopwatch();
    Stopwatch SwFe = new Stopwatch();

    string resultFrLst = "",resultFrIEnumrable, resultFe = "", Container = "";

    SwFr.Start();

    for (int itr = 0; itr < 100; itr++)
    {
        for (int i = 0; i < ListfromStream.Count(); i++)
        {
            Container = ListfromStream.ElementAt(i);
        }
    //the stop() was here , i was doing changes , so my mistake.
    }

   SwFr.Stop();
   resultFrLst = SwFr.Elapsed.ToString();
   //forgot to do this reset though still it is faster (x56??)
   SwFr.Reset();
   SwFr.Start();
        for(int itr = 0; itr<100; itr++)
        {
            for (int i = 0; i < IEnumFromStream.Count(); i++)
            {
                Container = IEnumFromStream.ElementAt(i);
            }
        }
    SwFr.Stop();
    resultFrIEnumrable = SwFr.Elapsed.ToString();

Update … final

taking out the counter to outside of the for loops ,

int counter = ..countfor both IEnumerable & List

then passed counter(int) as a count of total items as suggested by @ScottChamberlain .
re checked that every thing is in place, now the results are 5 % faster IEnumerable.
so that concludes , use by scenario – use case… no performance difference at all …

  • 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-06-15T23:32:00+00:00Added an answer on June 15, 2026 at 11:32 pm

    You have a few problems with your test, one is the IEnumFromStream.Count() inside the for loop, every time it want to get that value it must enumerate over the entire list to get the count and the value is not cached between loops. Move that call outside of the for loop and save the result in a int and use that value for the for loop, you will see a shorter time for your IEnumerable.

    Also the IEnumFromStream.ElementAt(i) behaves similarly to Count() it must iterate over the whole list up to i (eg: first time it goes 0, second time 0,1, third 0,1,2, and so on…) every time where List can jump directly to the index it needs. You should be working with the IEnumerator returned from GetEnumerator() instead.

    IEnumerable‘s and for loop’s don’t mix well. Use the correct tool for the job, either call GetEnumerator() and work with that or use it in a foreach loop.


    Now, I know a lot of you may be saying “But it is a interface it will be just mapping the calls and it should make no difference”, but there is a key thing, IEnumerable<T> Does not have a Count() or ElementAt() method!. Those methods are extension methods added by LINQ, and the LINQ classes do not know the underlying collection is a List, so it does what it knows the underlying object can do, and that is iterating over the list every time the method is called.


    IEnumerable using IEnumerator

    using(var enu = IEnumFromStream.GetEnumerator())
    {
        //You have to call "MoveNext()" once before getting "Current" the first time,
        //   this is done so you can have a nice clean while loop like this.
        while(enu.MoveNext())
        {
            Container = enu.Current;
        }
    }
    

    The above code is basically the same thing as

    foreach(var enu in IEnumFromStream)
    {
        Container = enu;
    }
    

    The important thing to remember is IEnumerable‘s do not have a length, in fact they can be infinitely long. There is a whole field of computer science on detecting a infinitely long IEnumerable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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
For some reason, after submitting a string like this Jack’s Spindle from a text
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

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.