473,385 Members | 1,727 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,385 software developers and data experts.

2D array indexing very slow

In C# why is:

int[] myArray = new int[5000,5000];

for (int y = 0; y < 5000; y++)
{
for (int x = 0; x < 5000; x++)
{
myArray[x, y] = 1;
}
}

So slow!!!

I think It's the indexing into the array which is causing
performance hits in my application but why? Either this
or am I simply using too big an array to expect a
quick operation?

If it's a problem with C# is there a way around this?

Many thanks.

Tristan.
Nov 15 '05 #1
3 3777
tr************@yahoo.co.uk (Tristan) wrote in
news:7f**************************@posting.google.c om:
In C# why is:

int[] myArray = new int[5000,5000];

for (int y = 0; y < 5000; y++)
{
for (int x = 0; x < 5000; x++)
{
myArray[x, y] = 1;
}
}

So slow!!!

I think It's the indexing into the array which is causing
performance hits in my application but why? Either this
or am I simply using too big an array to expect a
quick operation?

If it's a problem with C# is there a way around this?

Many thanks.

Tristan.


i always use the following code (which is quite fast btw. although it
doesn't do anything else than mapping the 2 dimensional addressing mode
on linear addressing):

int xsize = 5000;
int ysize = 5000;
int[] myArray = new int[xsize*ysize];

for(int y=0; y<ysize; y++)
for(int x=0; x<xsize; x++)
myArray[y*xsize + x] = 1;

greets
Peter

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD MCDBA
CAI/RS CASE/RS IAT

------ooo---OOO---ooo------
Nov 15 '05 #2
Peter's reduces it by a factor of 10 when i tried it out! I used:

private void button1_Click(object sender, System.EventArgs e)
{
DateTime start = DateTime.Now;
/*int[,] myArray = new int[5000,5000];

for (int y = 0; y < 5000; y++)
{
for (int x = 0; x < 5000; x++)
{
myArray[x, y] = 1;
}
}*/
int xsize = 5000;
int ysize = 5000;
int[] myArray = new int[xsize*ysize];

for(int y=0; y<ysize; y++)
for(int x=0; x<xsize; x++)
myArray[y*xsize + x] = 1;
DateTime end = DateTime.Now;

MessageBox.Show(end.Ticks - start.Ticks +"");

}

"Peter Koen" <koen-newsreply&snusnu.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
tr************@yahoo.co.uk (Tristan) wrote in
news:7f**************************@posting.google.c om:
In C# why is:

int[] myArray = new int[5000,5000];

for (int y = 0; y < 5000; y++)
{
for (int x = 0; x < 5000; x++)
{
myArray[x, y] = 1;
}
}

So slow!!!

I think It's the indexing into the array which is causing
performance hits in my application but why? Either this
or am I simply using too big an array to expect a
quick operation?

If it's a problem with C# is there a way around this?

Many thanks.

Tristan.


i always use the following code (which is quite fast btw. although it
doesn't do anything else than mapping the 2 dimensional addressing mode
on linear addressing):

int xsize = 5000;
int ysize = 5000;
int[] myArray = new int[xsize*ysize];

for(int y=0; y<ysize; y++)
for(int x=0; x<xsize; x++)
myArray[y*xsize + x] = 1;

greets
Peter

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD MCDBA
CAI/RS CASE/RS IAT

------ooo---OOO---ooo------

Nov 15 '05 #3
Chris Small <chris@sloppycode[remove> wrote:
Peter's reduces it by a factor of 10 when i tried it out! I used:

private void button1_Click(object sender, System.EventArgs e)
{
DateTime start = DateTime.Now;
/*int[,] myArray = new int[5000,5000];

for (int y = 0; y < 5000; y++)
{
for (int x = 0; x < 5000; x++)
{
myArray[x, y] = 1;
}
}*/
int xsize = 5000;
int ysize = 5000;
int[] myArray = new int[xsize*ysize];

for(int y=0; y<ysize; y++)
for(int x=0; x<xsize; x++)
myArray[y*xsize + x] = 1;
DateTime end = DateTime.Now;

MessageBox.Show(end.Ticks - start.Ticks +"");

}


(I've never understood why people are obsessed with using GUIs for no
good reason... See http://www.pobox.com/~skeet/csharp/benchmark.html
for a simple benchmarking framework.)

Anyway, there's a very good reason why Peter's code is so much faster
than the original, and it's mostly *not* because of the 2D array. It's
because of locality of reference. The 1D version is going through the
memory in one pass, byte by byte. The 2D version is skipping through
the array, the first byte then the 5001st, then the 10001st, etc, then
back to the second byte, etc.

Benchmarking the 2D version which does the same as the 1D version in
terms of ordering, it's far, far closer - less than a factor of 2
difference, which is much less likely to be a significant factor in
real world apps where values in memory tend to actually be *used*.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

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

Similar topics

6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
1
by: jon wayne | last post by:
Hi Am trying to replace the classic switch case construct (am workng on a telecom stack devlpmnt)with an array of function ptrs. The problm am facing is that of indexing. - the case vals can be...
32
by: Carson | last post by:
Hi , Is there a very efficient way to set a double array to 0 ? (I have tried memset, but the result doesn't look correct.) Carson
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
10
by: Dennis Myrén | last post by:
Hi. I have an array of struct. My question is, if i do: STRUCT a = new STRUCT ; STRUCT s = new STRUCT(); a = s; since STRUCT is a value type, when assigning element 0 of the STRUCT array...
4
by: Grace Fang | last post by:
Hi, I am writing code to sort the columns according to the sum of each column. The dataset is huge (50k rows x 300k cols), so i need to read line by line and do the summation to avoid the...
4
by: Amar | last post by:
Hi All, I need to select data from a database table containing huge amount of data. Now I am storing data using one primary key and I am just using simple select statement, and this process...
9
by: usao | last post by:
Does anyone have an example of how to create a class which describes and array, such that I can use the subscript operator on both the left and right side of an assignment statement? This is as...
3
by: Rüdiger Werner | last post by:
Hello! Out of curiosity and to learn a little bit about the numpy package i've tryed to implement a vectorised version of the 'Sieve of Zakiya'. While the code itself works fine it is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.