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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:32:55+00:00 2026-05-12T09:32:55+00:00

This is more of an academic question about performance than a realistic ‘what should

  • 0

This is more of an academic question about performance than a realistic ‘what should I use’ but I’m curious as I don’t dabble much in IL at all to see what’s constructed and I don’t have a large dataset on hand to profile against.

So which is faster:

List<myObject> objs = SomeHowGetList();
List<string> strings = new List<string>();
foreach (MyObject o in objs)
{
    if (o.Field == "something")
        strings.Add(o.Field);
}

or:

List<myObject> objs = SomeHowGetList();
List<string> strings = new List<string>();
string s;
foreach (MyObject o in objs)
{
    s = o.Field;
    if (s == "something")
        strings.Add(s);
}

Keep in mind that I don’t really want to know the performance impact of the string.Add(s) (as whatever operation needs to be done can’t really be changed), just the performance difference between setting s each iteration (let’s say that s can be any primitive type or string) verses calling the getter on the object each iteration.

  • 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-12T09:32:56+00:00Added an answer on May 12, 2026 at 9:32 am

    Your first option is noticeably faster in my tests. I’m such flip flopper! Seriously though, some comments were made about the code in my original test. Here’s the updated code that shows option 2 being faster.

        class Foo
        {
            public string Bar { get; set; }
    
            public static List<Foo> FooMeUp()
            {
                var foos = new List<Foo>();
    
                for (int i = 0; i < 10000000; i++)
                {
                    foos.Add(new Foo() { Bar = (i % 2 == 0) ? "something" : i.ToString() });
                }
    
                return foos;
            }
        }
    
        static void Main(string[] args)
        {
    
            var foos = Foo.FooMeUp();
            var strings = new List<string>();
    
            Stopwatch sw = Stopwatch.StartNew();
    
            foreach (Foo o in foos)
            {
                if (o.Bar == "something")
                {
                    strings.Add(o.Bar);
                }
            }
    
            sw.Stop();
            Console.WriteLine("It took {0}", sw.ElapsedMilliseconds);
    
            strings.Clear();
            sw = Stopwatch.StartNew();
    
            foreach (Foo o in foos)
            {
                var s = o.Bar;
                if (s == "something")
                {
                    strings.Add(s);
                }
            }
    
            sw.Stop();
            Console.WriteLine("It took {0}", sw.ElapsedMilliseconds);
            Console.ReadLine();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an almost academic question but I'm curious as to its answer. Suppose
This is an academic question, more than a practical one. I'm trying to delve
This is really more of an academic question, but where is this function defined?
This is more of an academic inquiry than a practical question. Are there any
My knowledge of C++ at this point is more academic than anything else. In
Just an academic question. I'm curious what version of this code is better (faster)
This is an academic question (I'm not necessarily planning on doing it) but I
Somewhat of an academic question, but I ran into this while writing some unit
I'm interested in this from more than an academic standpoint rather than a practical
I know this is a rather academic question, but I wonder how one would

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.