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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:05:25+00:00 2026-05-11T03:05:25+00:00

Edit: I apologize everybody. I used the term jagged array when I actually meant

  • 0

Edit: I apologize everybody. I used the term ‘jagged array’ when I actually meant to say ‘multi-dimensional array’ (as can be seen in my example below). I apologize for using the incorrect name. I actually found jagged arrays to be faster than multi-dimensional ones! I have added my measurements for jagged arrays.

I was trying to use a jagged multi-dimensional array today, when I noticed that it’s performance is not as I would have expected. Using a single-dimensional array and manually calculating indices was much faster (almost two times) than using a 2D array. I wrote a test using 1024*1024 arrays (initialized to random values), for 1000 iterations, and I got the following results on my machine:

sum(double[], int): 2738 ms (100%) sum(double[,]):     5019 ms (183%) sum(double[][]):    2540 ms ( 93%) 

This is my test code:

public static double sum(double[] d, int l1) {     // assuming the array is rectangular     double sum = 0;     int l2 = d.Length / l1;     for (int i = 0; i < l1; ++i)         for (int j = 0; j < l2; ++j)             sum += d[i * l2 + j];     return sum; }  public static double sum(double[,] d) {     double sum = 0;     int l1 = d.GetLength(0);     int l2 = d.GetLength(1);     for (int i = 0; i < l1; ++i)         for (int j = 0; j < l2; ++j)             sum += d[i, j];     return sum; }  public static double sum(double[][] d) {     double sum = 0;     for (int i = 0; i < d.Length; ++i)         for (int j = 0; j < d[i].Length; ++j)             sum += d[i][j];     return sum; }  public static void Main() {     Random random = new Random();     const int l1  = 1024, l2 = 1024;     double[ ] d1  = new double[l1 * l2];     double[,] d2  = new double[l1 , l2];     double[][] d3 = new double[l1][];      for (int i = 0; i < l1; ++i) {         d3[i] = new double[l2];         for (int j = 0; j < l2; ++j)             d3[i][j] = d2[i, j] = d1[i * l2 + j] = random.NextDouble();     }     //     const int iterations = 1000;     TestTime(sum, d1, l1, iterations);     TestTime(sum, d2, iterations);     TestTime(sum, d3, iterations); } 

Further investigation showed that the IL for the second method is 23% larger than that of the first method. (Code size 68 vs 52.) This is mostly due to calls to System.Array::GetLength(int). The compiler also emits calls to Array::Get for the jagged multi-dimensional array, whereas it simply calls ldelem for the simple array.

So I am wondering, why is access through multi-dimensional arrays slower than normal arrays? I would have assumed the compiler (or JIT) would do something similar to what I did in my first method, but this was not actually the case.

Could you plese help me understand why this is happening the way it is?


Update: Following Henk Holterman’s suggestion, here is the implementation of TestTime:

public static void TestTime<T, TR>(Func<T, TR> action, T obj,                                    int iterations) {     Stopwatch stopwatch = Stopwatch.StartNew();     for (int i = 0; i < iterations; ++i)         action(obj);     Console.WriteLine(action.Method.Name + ' took ' + stopwatch.Elapsed); }  public static void TestTime<T1, T2, TR>(Func<T1, T2, TR> action, T1 obj1,                                         T2 obj2, int iterations) {     Stopwatch stopwatch = Stopwatch.StartNew();     for (int i = 0; i < iterations; ++i)         action(obj1, obj2);     Console.WriteLine(action.Method.Name + ' took ' + stopwatch.Elapsed); } 
  • 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. 2026-05-11T03:05:25+00:00Added an answer on May 11, 2026 at 3:05 am

    Single dimensional arrays with a lower bound of 0 are a different type to either multi-dimensional or non-0 lower bound arrays within IL (vector vs array IIRC). vector is simpler to work with – to get to element x, you just do pointer + size * x. For an array, you have to do pointer + size * (x-lower bound) for a single dimensional array, and yet more arithmetic for each dimension you add.

    Basically the CLR is optimised for the vastly more common case.

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

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm fairly sure that standard Flash projectors can not do… May 12, 2026 at 12:09 am
  • Editorial Team
    Editorial Team added an answer String computername=InetAddress.getLocalHost().getHostName(); System.out.println(computername); May 12, 2026 at 12:09 am
  • Editorial Team
    Editorial Team added an answer You want to look up boxing/unboxing. Boxing May 12, 2026 at 12:09 am

Related Questions

I have a question, and I'm going to tag this subjective since that's what
Would anyone be able to tell me how to pull the server name out
A) Question below is based on the assumption that controls are always binded to
EDIT 1 I apologize but after reading the 2 suggested articles I still don't

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.