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 for the compiler and the best practice for checking whether
Which is more efficient? SELECT theField FROM theTable GROUP BY theField or SELECT DISTINCT
Which Database table Schema is more efficient and why? Users (UserID, UserName, CompamyId) Companies
Question of the century? I basically want to know which would be more efficient
I have a number of tracks recorded by a GPS, which more formally can
Yahoo has separate map for India ( which has more details than the regular
I have an excel file which has more than 65536 rows. However, I can
I want to create a query which has more than 3-4 Expression.Or ? But
Functional programming seems to be a paradigm in computer science which has more and

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.