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

  • Home
  • SEARCH
  • 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 8402543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:05:58+00:00 2026-06-09T22:05:58+00:00

I’m trying to work out the fastest way to iterate a multidimensional C# array.

  • 0

I’m trying to work out the fastest way to iterate a multidimensional C# array. I have stripped out all the domain code to focus on the problem. At the moment, this executes in 1.86 seconds, performing about 25,000,000 iterations in that time, processing 5000 array elements. I’m setting myself the goal of bring 1.86s down as much as I can in 2 days 🙂

In the real world, it’ll be more like 50,000² to process.

I have tried using PLINQ, but it seems the threading overheads actually make it slowers (comes it at 3.48s).

I’m thinking unsafe C# might be the way to go, however, before I go down that road, I’d appreciate any thoughts about how to improve performance? I haven’t done unsafe C# before, so I’m not sure whether it’d be a performance gain?!

Console.WriteLine("started");
        var sw = System.Diagnostics.Stopwatch.StartNew();
        long iterations = 0;
        string[] data = new string[5000];
        string[] data2 = new string[5000];
        string[] data3 = new string[5000];

        int ubound = data.GetUpperBound(0);
        for (int i = 0; i <= ubound; i++)
        {
            string d1 = data[i];
            string d2 = data2[i];
            string d3 = data3[i];

            for (int j = 0; j < ubound; j++)
            {
                string e1 = data[j];
                string e2 = data2[j];
                string e3 = data3[j];
                Interlocked.Increment(ref iterations);
            }

            Interlocked.Increment(ref iterations);
        }
        Console.WriteLine("Finished {0} iterations in {1} seconds", iterations, sw.Elapsed.TotalSeconds);
  • 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-09T22:06:00+00:00Added an answer on June 9, 2026 at 10:06 pm

    Instead of a multidimensional array, go for a flat array and use maths to address it:

    Console.WriteLine("started");
    var sw = System.Diagnostics.Stopwatch.StartNew();
    long iterations = 0;
    
    var width=5000;
    var height=3;
    string[] data = new string[width*height];
    for (int i = 0; i < width; i++)
    {
        string d1 = data[i];
        string d2 = data[width+i];
        string d3 = data[width*2+i];
    
        for (int j = 0; j < width; j++)
        {
            string e1 = data[j];
            string e2 = data[width+j];
            string e3 = data[width*2+j];
            Interlocked.Increment(ref iterations);
        }
        //});
    
        Interlocked.Increment(ref iterations);
    }
    Console.WriteLine("Finished {0} iterations in {1} seconds", iterations, sw.Elapsed.TotalSeconds);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.