Array access performance (safe code)

1. How the CLR divides arrays?

2. Which type of arrays is faster access?

3. Why access to the SZ arrays is faster?

4. What type of arrays the C# also supports? Why the access to jagged arrays is a bit faster than access to rectangular arrays?

The CLR divides arrays into two types:

1.     SZ arrays(Single dimension zero based arrays).

2.     Multiple dimension zero based or non zero based arrays.

Lets look at the example:

 //Creation of a SZ(single dimension zero based bound) array. 

string[] szarray = new string[5];

Console.WriteLine(szarray);

Console.WriteLine(Array.CreateInstance(typeof(String), new int[]{5}, new int[]{0}));

 

In the both cases the result will be the same – System.String[];

 

//Creation of a single dimension 1 based bound array.

Console.WriteLine(Array.CreateInstance(typeof(String), new int[] { 5 }, new int[] { 1 }));

 

In this case the result will be System.String[*];

 

//Creation of a 2 dimension zero based bound array.

string[,] ndimarray = new string[5,5];

Console.WriteLine(ndimarray);            

Console.WriteLine(Array.CreateInstance(typeof(String), new int[] { 5, 5 }, new int[] { 0, 0 }));

 

//Creation of a 2 dimension 1 based bound array.

Console.WriteLine(Array.CreateInstance(typeof(String), new int[] { 5, 5 }, new int[] { 1, 1 }));

In the both last cases the result will be the same – System.String[,];

C# doensn’t allow to get an access to create varible type of System.String[*] implicitly, but it is possible with helps of the methods SetValue and GetValue

 

Array var = Array.CreateInstance(typeof(String), new int[] { 5 }, new int[] { 1 });

var.SetValue("1", 1);           

var.GetValue(1);

 

This type of access isn’t fast as usual access in the C#.

The CLR always treats all multidimensional arrays as they are not zero based array.

 

An access to the single dimension zero based arrays is always faster, because the C# compiler emits special IL instructions, which cause the JITter to emit optimized code to get an access to the SZ arrays. For example in this code the JITer makes two optimizations:

 

            int[] test = new int[5];

            for (int i = 0; i < test.Length; i++)

//Something TO DO

 

First of them it is range validation. The CLR makes range-checking out of the loop.

And second, JITter recognizes SZ arrays and doesn’t produce subtraction of the low-bound index to get an access to an array.

Also in this code the C# caches in the memory the value of the test.Length that makes enumeration a little bit faster.

 

The C# also supports jagged arrays which are arrays of arrays. Zero based, single dimensioned jagged arrays have the same performance like vectors.    

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