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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:04:34+00:00 2026-06-12T07:04:34+00:00

I am trying to optimize generic lists arithmetic operation. I have 3 lists of

  • 0

I am trying to optimize generic lists arithmetic operation. I have 3 lists of nullable double as defined below.

List<double?> list1 = new List<double?>();
List<double?> list2 = new List<double?>();
List<double?> listResult = new List<double?>();

int recordCount = list1.Count > list2.Count ? list2.Count : list1.Count;

for (int index = 0; index < recordCount; index++)
{
      double? result = list1[index] + list2[index];
      listResult.Add(result);
}

Is there any way to make this operation to run faster if I have huge lists?

Thanks for your input.

  • 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-12T07:04:35+00:00Added an answer on June 12, 2026 at 7:04 am

    Is there any way to make this operation to run faster if I have huge lists?

    You could move your list creation for the results until after your count:

    List<double?> list1 = new List<double?>();
    List<double?> list2 = new List<double?>();
    
    int recordCount = list1.Count > list2.Count ? list2.Count : list1.Count;
    List<double?> listResult = new List<double?>(recordCount);
    

    This would let you specify the exact capacity necessary for the results, and avoid reallocations within the list itself. For “huge lists” this is likely one of the slowest portions, as the memory allocations and copies as the list gets large will be the slowest operation here.

    Also, if the calculation is simple, you could potentially use multiple cores:

    List<double?> list1 = new List<double?>();
    List<double?> list2 = new List<double?>();
    
    int recordCount = list1.Count > list2.Count ? list2.Count : list1.Count;
    
    var results = new double?[recordCount]; // Use an array here
    
    Parallel.For(0, recordCount, index => 
        {
            double? result = list1[index] + list2[index];
            results[index] = result;
        });
    

    Given that the “work” is so simple here, you probably actually would need a custom partitioner to get the most out of parallelism (see How to: Speed Up Small Loop Bodies for details):

    var results = new double?[recordCount]; // Use an array here
    var rangePartitioner = Partitioner.Create(0, recordCount);
    
    Parallel.ForEach(rangePartitioner, range => 
        {
            for (int index = range.Item1; index < range.Item2; index++)
            {
                results[index] = list1[index] + list2[index];
            }
        });
    

    If this isn’t a bottleneck, however, you could use LINQ to do this as a one-liner:

    var results = list1.Zip(list2, (one, two) => one + two).ToList();
    

    However, this will be (very slightly) less efficient than handling the looping yourself, if performance is really a bottleneck.

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

Sidebar

Related Questions

I'm trying to optimize my code using Neon intrinsics. I have a 24-bit rotation
I'm currently trying to optimize my program. I have a large database which consists
I'm trying to optimize a SQL Server. I have some experience with Mysql and
In our product we have a generic search engine, and trying to optimze the
I have been trying to optimize some code which handles raw pixel data. Currently
I have been trying to optimize an ASP.NET C# application for a few days
I'm trying to optimize code that I have. In order to do that, I
I'm trying to optimize query performance and have had to resort to using optimizer
I am trying to optimize this query as good as possible,but still i am
I'm trying to optimize the memory usage of an iOS app, and I'd like

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.