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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:26:12+00:00 2026-06-02T19:26:12+00:00

A developer friend of mine tells me that loops are much faster using delegates,

  • 0

A developer friend of mine tells me that loops are much faster using delegates, and I’d like to benchmark it, but I’m having trouble connecting the dots on how it works.

Consider the following balance calculator. This basically takes a list of accounts and adds the initial value (starting balance) if it exists to the total credits value if it exist and subtracts the total debits value for each account:

    private static IDictionary<string, decimal> CalculateBalances(
        IDictionary<string, decimal> initialValue, 
        IDictionary<string, decimal> credits, 
        IDictionary<string, decimal> debits)
    {
        var r = new Dictionary<string, decimal>();

        foreach (var key in initialValue.Select(k => k.Key)
            .Concat(credits.Select(k => k.Key))
            .Concat(debits.Select(k => k.Key))
            .Distinct())
        {
            r.Add(key,
                (initialValue.ContainsKey(key) ? initialValue[key] : 0M)
                + (credits.ContainsKey(key) ? credits[key] : 0M)
                - (debits.ContainsKey(key) ? debits[key] : 0M)
                );
        }

        return r;
    }

This is fairly performant on small to medium account lists, but would using delegates be faster? And frankly, delegate logic seems to be operating at right angles to my thought processes, because I’m scratching my head how to even write this.

Can anyone offer a way to rewrite this using delegates?

  • 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-02T19:26:13+00:00Added an answer on June 2, 2026 at 7:26 pm

    I’m assuming your friend is referring to something like the ForEach method on the List<T> class. The short answer to your question is no.

    The equivalent syntax would be:

    initialValue.Select(k => k.Key)
                .Concat(credits.Select(k => k.Key))
                .Concat(debits.Select(k => k.Key))
                .Distinct()
                .ToList()
                .ForEach(var => r.Add(key,
                    (initialValue.ContainsKey(key) ? initialValue[key] : 0M)
                    + (credits.ContainsKey(key) ? credits[key] : 0M)
                    - (debits.ContainsKey(key) ? debits[key] : 0M)
                    ));
    

    This is in no way better than the way you have it above. It is both slower and more difficult to read. Delegate invocation is slower than ordinary method invocation. The syntax you have above is both faster and easier to read.

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

Sidebar

Related Questions

A friend and developer I respect recently advised that I avoid using 'Includes' in
I'm working on a game and I'd like to give a non-developer friend a
I am a .net developer. A friend of mine is planning to outsource a
A friend of mine is making the jump from graphic designer to game developer,
I'm not typically a web developer, but I'm a decent enough coder that I
I have a friend who is was a serious Linux developer but now he's
My friend has a website built on wordpress and the developer has left the
I developed a class that compiles to a dll and my friend uses this
a friend and I develop a web application for conferences management that uses hibernate
I develop code using .net, and sometimes using ruby. My friend wants me to

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.