Performance of Non-Zero lower bound Single Dimensional Array

Today while returning from office, I had a very good conversation with one of mine friend Sunny Raheja. We were discussing here and there and suddenly he asked to me

“Hey DJ, is there any difference in performance of single dimensional array if I don’t give lower bound as 0 or if lower bound is non-zero“

I thought for a while and replied to him tune in for my next blog post Smile

I was not very sure about the answer at that point of time so as soon as I reached home, I took CLR via C# because I was sure; I would get answer in this book.

So answer of above question goes like this,

clip_image001[4]

Single Dimensional array with Zero based index is having better performance.

clip_image002[4]

Single Dimensional array with Non Zero based index is having slow access of the array values.

To support above statement let us run below code,

clip_image004

In above code

  1. We are creating an Integer Array
  2. Size of Array is 2
  3. Lower bound is 0

On running Output we would get,

clip_image006

Now let us create an array with lower bound 1

clip_image008

In above code

  1. We are creating an Integer Array
  2. Size of Array is 2
  3. Lower bound is 1

On running Output we would get,

clip_image010

If you see the difference in both output; There is * in type of non -Zero based array. So by looking at type (*) complier knows about non-zero index array.

Since we know there is nothing called (*) in c# and CLR does not support declaration or access of variable with *.

So to access elements of non-zero based, Array’s GetValue() and SetValue() method can be used and it would reduce the performance.

I hope Sunny would be satisfied by this answer Smile with tongue out

Program.cs

using System;

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {

            Array myArray;
            myArray = new string[0];
            Console.WriteLine(myArray.GetType());
            Console.ReadKey(true);

            myArray = Array.CreateInstance(typeof(string),
                      new Int32[] { 2 },
                      new Int32[] { 0 });
            Console.WriteLine(myArray.GetType());
            Console.ReadKey(true);

            myArray = Array.CreateInstance(typeof(string),
                       new Int32[] { 2 },
                       new Int32[] { 1 });
            Console.WriteLine(myArray.GetType());

            Console.ReadKey(true);
        }
    }
}

Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Tagged with: , ,
Posted in CSharp, Miscellaneous
6 comments on “Performance of Non-Zero lower bound Single Dimensional Array
  1. Sunny Raheja says:

    Hey… thanks man..
    good explanation :)

  2. Sanjay says:

    Nice sir. Well Explained

  3. Anonymous says:

    Very userful tips, keep it up.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

  • after 2 sixes of Sami .. I say LOL -> Virat KOHLI ... happy to see Kohli the ILL ATTITUDE MAN LOOSING .. 6 hours ago
  • It does not matter from where are you coming ? only does matter where are you going ? #djsays 6 hours ago
  • With people who participated in WCF day .. Great hosting by @CsharpCorner amd great leadership of @mcbkruse http://t.co/HkHjCdveJC 7 hours ago
  • RT @petekrueger: I've been using @Icenium. I would definitely recommend checking it out if you're tackling cross-platform mobile: http://t.… 7 hours ago
  • मुझे ग़म है की मैंने जिंदगी में कुछ नहीं पाया , ये ग़म दिल से निकल जाये अगर तुम मिलने आ जाओ 9 hours ago
Categories
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2013
Follow

Get every new post delivered to your Inbox.

Join 2,123 other followers

%d bloggers like this: