473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Are even square jagged arrays faster?

I done a test and i really do not know the reason why a jagged array who
has the same number of elements as a multidimensional array is faster
here is my test. I assign a value and do a small calculation. Even if i
initialise the jagged array inside the function it is still much faster.
Are these results correct?. If i put the initialisation loop in the
constructor its ridiculously faster but even here its 4 times
faster...is this correct?

private int[,] multi= new int[2000, 2000];
private int[][] jagged = new int[2000][];

private void btnArrayPerformance_Click(object sender, System.EventArgs
e)
{
for(int x = 0;x < 2000;x++)
{
jagged[x] = new int[2000];
}
for(i = 0;i < 2000;i++)
for(j = 0;j < 2000;j++)
{
jagged[j][k] = 8;
jagged[j][k] = (8 * 2);
}
}

private void btnMulti_Click(object sender, System.EventArgs e)
{
for(i = 0;i < 2000;i++)
for(j = 0;j < 2000;j++)
{
multi[j,k] = 8;
multi[j,k] = (8 * 2);
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
1 2691
Hi,

Yes, is totally true,and even faster if you access first to your first
dimension and then the second if a nested for.

The reason for jagged arrays superior performance is that the CLR is
optimized to access elements of vectors and, even if the accessing a jagged
array's element requires two vector access, the combined operatin is still
faster than a two-dimensional array. Also, jagged arrays consumes less memory
and their elements can be referenced faster.

From the Assembler point of view remember to check your access
[x,y,z]

for (x)
for (y)
for (z)

You will be surprise that the order can double the speed.

cheers
Salva

"James dean" wrote:
I done a test and i really do not know the reason why a jagged array who
has the same number of elements as a multidimensional array is faster
here is my test. I assign a value and do a small calculation. Even if i
initialise the jagged array inside the function it is still much faster.
Are these results correct?. If i put the initialisation loop in the
constructor its ridiculously faster but even here its 4 times
faster...is this correct?

private int[,] multi= new int[2000, 2000];
private int[][] jagged = new int[2000][];

private void btnArrayPerformance_Click(object sender, System.EventArgs
e)
{
for(int x = 0;x < 2000;x++)
{
jagged[x] = new int[2000];
}
for(i = 0;i < 2000;i++)
for(j = 0;j < 2000;j++)
{
jagged[j][k] = 8;
jagged[j][k] = (8 * 2);
}
}

private void btnMulti_Click(object sender, System.EventArgs e)
{
for(i = 0;i < 2000;i++)
for(j = 0;j < 2000;j++)
{
multi[j,k] = 8;
multi[j,k] = (8 * 2);
}
}

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: James dean | last post by:
I have created algorithms in C# unsafe code and have fixed the arrays in memory for optimum performance. I use multidimensional arrays rather than jagged arrays. The algorithms i use usually read a...
3
by: PaulW | last post by:
Does anyone know the forthcoming Whidbey release of the C++ compiler support Jagged (or Ragged if you prefer) Arrays If so how?
1
by: Chris Wood | last post by:
I need to do a variable argument list in Managed C++, where each argument is a double array. I believe that this is impossible, because to do so I would need to make a jagged matrix of doubles,...
3
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string...
1
by: xllx.relient.xllx | last post by:
Hi, I have two questions: 1.)Is it true that an rectangular array is really just an single dimensional array that lets itself be treated as a multi-dimensional array? For example the...
2
by: deko | last post by:
I trying to create a jagged array of two arrays, with the second array being an array of two-dimensional arrays. A graphical representation might look like this: x y y y x y x y y x ...
5
by: TS | last post by:
is there some code somewhere that does this? i have a jagged array that is not jagged, it has an equal number of rows and columns in each array so it should convert but want to get the code to do...
0
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hello, i have a problem with jagged arrays and a webservice. I try to call a webservice with an array of a class that contains jagged arrays. Here is an example: class MyClass{ string...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.