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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:30:03+00:00 2026-05-12T08:30:03+00:00

Which is more efficient? //Option 1 foreach (var q in baseQuery) { m_TotalCashDeposit +=

  • 0

Which is more efficient?

//Option 1
foreach (var q in baseQuery)
{
  m_TotalCashDeposit += q.deposit.Cash
  m_TotalCheckDeposit += q.deposit.Check
  m_TotalCashWithdrawal += q.withdraw.Cash
  m_TotalCheckWithdrawal += q.withdraw.Check
}

//Option 2
m_TotalCashDeposit = baseQuery.Sum(q => q.deposit.Cash);
m_TotalCheckDeposit = baseQuery.Sum(q => q.deposit.Check);
m_TotalCashWithdrawal = baseQuery.Sum(q => q.withdraw.Cash);
m_TotalCheckWithdrawal = baseQuery.Sum(q => q.withdraw.Check);

I guess what I’m asking is, calling Sum will basically enumerate over the list right? So if I call Sum four times, isn’t that enumerating over the list four times? Wouldn’t it be more efficient to just do a foreach instead so I only have to enumerate the list once?

  • 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-12T08:30:03+00:00Added an answer on May 12, 2026 at 8:30 am

    It might, and it might not, it depends.

    The only sure way to know is to actually measure it.

    To do that, use BenchmarkDotNet, here’s an example which you can run in LINQPad or a console application:

    void Main()
    {
        BenchmarkSwitcher.FromAssembly(GetType().Assembly).RunAll();
    }
    
    public class Benchmarks
    {
        [Benchmark]
        public void Option1()
        {
    //        foreach (var q in baseQuery)
    //        {
    //            m_TotalCashDeposit += q.deposit.Cash;
    //            m_TotalCheckDeposit += q.deposit.Check;
    //            m_TotalCashWithdrawal += q.withdraw.Cash;
    //            m_TotalCheckWithdrawal += q.withdraw.Check;
    //        }
        }
    
        [Benchmark]
        public void Option2()
        {
    //        m_TotalCashDeposit = baseQuery.Sum(q => q.deposit.Cash);
    //        m_TotalCheckDeposit = baseQuery.Sum(q => q.deposit.Check);
    //        m_TotalCashWithdrawal = baseQuery.Sum(q => q.withdraw.Cash);
    //        m_TotalCheckWithdrawal = baseQuery.Sum(q => q.withdraw.Check);
        }
    }
    

    BenchmarkDotNet is a powerful library for measuring performance, and is much more accurate than simply using Stopwatch, as it will use statistically correct approaches and methods, and also take such things as JITting and GC into account.


    Now that I’m older and wiser I no longer belive using Stopwatch is a good way to measure performance. I won’t remove the old answer, as google and similar links may lead people here looking for how to use Stopwatch to measure performance, but I hope I have added a better approach above.

    Original answer below

    Simple code to measure it:

    Stopwatch sw = new Stopwatch();
    sw.Start();
    // your code here
    sw.Stop();
    Debug.WriteLine("Time taken: " + sw.ElapsedMilliseconds + " ms");
    sw.Reset(); // in case you have more code below that reuses sw
    

    You should run the code multiple times to avoid having JITting having too large an effect on your timings.

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

Sidebar

Related Questions

In C# which is more memory efficient: Option #1 or Option #2? public void
which is more efficient (when managing over 100K records): A. Mysql SELECT * FROM
I am wondering which is more efficient, to store temporary data (related to that
I have to store key value pairs in java which is more memory efficient
Which of these queries is more efficient, and would a modern DBMS (like SQL
Which set of selectors is more efficient? 1 $('#parent_element span.class1').do_something1(); $('#parent_element span.class2').do_something2(); $('#parent_element span.class3').do_something3();
My real Question is which of these is more efficient in terms of loading
Is there any time in which a reference type is more efficient than a
As a beginning iOS developer, which approach allows for quicker more efficient development?
Wondering which would be the more efficient technique for indexing my document's various timestamps

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.