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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:39:02+00:00 2026-06-06T17:39:02+00:00

I am writing some c# code that is currently supposed to run as fast

  • 0

I am writing some c# code that is currently supposed to run as fast as possible, usually taking up a single core at 100% for about 25min. I need the code to remain single core as the benefit of running this code across multiple cores will not be as great as running this project multiple times concurrently

The code in question is as follows:

public Double UpdateStuff(){

    ClassA[] CAArray = ClassA[*a very large number indeed*];
    Double Value = 0;
    int length = CAArray.Length;

    for (int i= 0; i< length ; i++)
        {
         Value += CAArray[i].ClassB.Value * CAArray[i].Multiplier;
        }  
    return Value;
}

This area of the code is responsible for 78% of the load of the application according to profilers and thus seems a good candidate for optimisation.

Note, the function has been changed from return type void to return type Double, this is pseudocode and not actual code to allow easier reading.

To Clarify: .net, c#4.0, visual studio 2010, target machine : windows server 2008 x64.

Edit: Further clarification: all variables in this context are public and not properties. The values in CAArray[i].ClassB.Value will be forever changing doubles that cannot be pair matched.

  • 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-06T17:39:03+00:00Added an answer on June 6, 2026 at 5:39 pm

    You should remove this:

    int length = CAArray.Length;
    

    and replace the loop with this:

    for (int i= 0; i < CAArray.Length; i++)
    {
        Value += CAArray[i].ClassB.Value * CAArray[i].Multiplier;
    } 
    

    Storing the length like your original code does actually slows down C# code (counter-intuitive, I know). This is because if you have Array.Length directly in the for loop, the jitter will skip doing an array bounds-check on each iteration of the loop.

    Also, I strongly suggest parallelizing this process. The simplest way to do this is

    CAArray.AsParallel().Sum(i => i.ClassB.Value * i.Multiplier);
    

    although you could potentially get even more speed without LINQ (though you then need to worry about the low level details of managing multiple threads).

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

Sidebar

Related Questions

i'm currently writing on some piece of code in android 2.1 that is supposed
I'm currently writing a piece of code that does some searches which returns IDisposable
I am currently writing some code that can access a separate workbook through an
I'm writing some code that will take a screenshot of another application, given its'
I am writing some code that will go through a file, edit it as
I'm writing some code that has a lot of substitution. There's a list<char> productions
I am writing some code that connects to a website, and using C#, and
I am writing some code that needs to be backwards compatible with EXISTING remoting
I'm writing some code that looks like this: while(true) { switch(msg->state) { case MSGTYPE:
I'm writing some code that takes a report from the mainframe and converts it

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.