David Overton's Blog and Discussion Site
This site is my way to share my views and general business and IT information with you about Microsoft, IT solutions for ISVs, technologists and businesses, large and small. I specialise in Windows Intune and SBS 2008.
This blog is purely the personal opinions of David Overton. If you can't find the information you were looking for e-mail me at admin@davidoverton.com.

To find out more about my Windows Intune BOOK - Microsoft Windows Intune 2.0: Quickstart Administration click here

To find out more about my SBS 2008 BOOK - Small Business Server 2008, Installation, Migration and Configuration click here

How to improve Array Performance in .Net programming
David Overton's Blog

Buy my books

Windows Intune:Quickstart Administration


This is the RAW book (Read as Written).
Click here for more information
Buy or pre-order today

SBS 2008 - Installation, Migration and Configuration

Small Business Server 2008 – Installation, Migration, and Configuration

Buy today in book or e-book form

Request a Review Copy

Twitter

Syndication

I have been known to write the odd program, often using a large number of arrays for some of the manipulation I do.  Multi-dimensional arrays is something I have done a lot of, so seeing this little tip from CodeGuru to speed them up and reduce memory footprint is a great benefit for me.  Below are a few snippets, but read the whole article to know more.

Jag It Up

What is a jagged array, and why does it have such an alarming name? I can't answer the second question, but I'll give the first one a shot. A jagged array is essentially just a one-dimensional array where each element contains its own one-dimension array. It's an array of arrays! (For those who just must jump ahead—you know who you are—yes, you can indeed have arrays of arrays of arrays through infinity or until your memory runs out. But, let's not go there now....)

How is an array of arrays different from a two-dimensional array? I'm glad you asked. This is where the jaggy bit comes in. In a two-dimensional array, you have a fixed number of elements in each dimension. It might be 10 by 10, providing 100 elements, for example. In a jagged array, your primary array has a fixed number of elements, but then each element array can have any number of elements. So, you could have a primary array with 10 elements, where the first element holds an array of 12 elements, the second has 7 elements, the third has 20, and so on. If you imagine the 10 by 10 array as a square, the right edge of that square becomes quite jagged when it is replaced with the array-of-arrays.

The benefit that immediately comes to mind is a savings in memory—you only use the number of elements needed for each element array, rather than giving a second dimension the maximum number of elements you'll ever need and then having a lot of blanks. The benefit that's not obvious is the accelerated processing time!

So, how do you declare a jagged array in your code? Here's an example:
Dim EmployeesByRegion()() As Integer = {_
   New Integer() { 12, 25, 7, 33, 2 }, _
   New Integer() { 15, 8, 23 }, _
   New Integer() { 5, 7, 3, 11, 4, 2 }  }

Int[][] employeesByRegion = new int[][] {
   new int[] { 12, 25, 7, 33, 2 },
   new int() { 15, 8, 23 },
   new int() { 5, 7, 3, 11, 4, 2 }  }

Each element in the primary array (named EmployeesByRegion) represents a single region and holds an array. Each element in the element arrays (there are three of them here and they are not named), hold the number of employees for an office within the region. So, in this case the first region has five offices, the second has three, and the third has six. The total number of employees in the first region is 12+25+7+33+2 or 79.

The syntax here is handy because it allows you to specify the element values in the array declaration without the need to specify the size of the dimensions, just as you would in a standard two-dimensional array.

Wanna Take a Ride in My New Jag?

So far so good. You ready to access, update, and generally use this thing? An example:

Console.WriteLine("Region #1, Office #3: ")
Console.WriteLine(EmployeesByRegion(0)(2))    ' Indexes are 0-based
Console.WriteLine("Region #3, Office #5 Hires a New Employee")
EmployeesByRegion((2)(4)) = EmployeesByRegion((2)(4)) + 1

Total = 0
For i=0 To 6
   Total = Total + EmployeesByRegion(2, i)
Next

Notice that, instead of using the (0,2) syntax, you place each dimension's index in its own set of parentheses. Other than that, the method of accessing and updating elements is the same as working with a two-dimensional array.

The difference is that the CLR is optimized for two kinds of data structures: elements and one-dimensional arrays (sometimes called vectors). Two-dimensional arrays take more processing time and effort by the CLR; therefore, your application's speed is compromised. However, although jagged arrays may seem like the same thing (or even more complex) they are, in fact, simpler for the CLR to manipulate, being really just a group of one-dimensional arrays.

ttfn

David


Posted Fri, Aug 31 2007 1:18 AM by David Overton
Filed under: ,

Add a Comment

(optional)  
(optional)
(required)  
Remember Me?

(c)David Overton 2006-13