Get Jagged!


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

.NET programming offers a rich pallet of tools to draw upon when creating data structures. For example, in many situations, collections provide a way to create cleaner, easier-to-write and maintain code. However, many developers are unaware that there is a significant performance cost for the luxury that collections bring. Using native-type arrays is significantly faster. And, if you are doing two-dimensional (or more) arryas, you can get another boost in performance by using jagged arrays.

In this article, I'll show you how to super-charge your application performance by implementing jagged arrays.

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!

You Don't Know Jag!

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)

Console.WriteLine("Total Employees in Region #3 Now: ")

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(int i=0, i<=6, i++)
   Total = Total + EmployeesByRegion(2, i);
Console.WriteLine("Total Employees in Region #3 Now: ");

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.

Hopefully, this short article has given you a perspective what jagged arrays are, how to use them and, most importantly, why they should be an important gun in your arsenal!

About the Author

Bill Hatfield

Bill Hatfield is the best-selling author of numerous books for developers, including ASP.NET For Dummies, Active Server Pages For Dummies (on Classic ASP) and Visual InterDev For Dummies. He is also the editor of Visual Studio .NET Developer, a monthly technical journal from Pinnacle. He's an experienced corporate trainer and works in Indianapolis, IN where he also co-founded the local .NET group, the Indianapolis .NET Developers Association.


  • Not so simple as it is...

    Posted by rafal.maciag on 08/27/2007 02:15pm

    1) If you use arrays of arrays then you call OS depending on GC from 0 to even n+1 times where n is the length of the first array. If sub-arrays are large then it is more possible that OS will be call each time you create a sub-array. (Changing the context takes some CPU time). 2) Creating sub-arrays will cause the GC to work harder to free memory, than it would be when using 2D array. 3) If I can imagine using this kind of technique is suitable when sub-arrays are different, but constant length. If they are not constant during the execution, then the same - it would be advisable to use f.e. List>, just for the user friendly features that List class provides (List is an array inside indeed). 4) I agree that 2D and more collection are faster in native code. 5) I suggest publishing some performance test results in this kind of article when talking about speed....

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date