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

2D arrays

instead of using 2D arrays:

int A[m][n];

I heard it was faster/preferred to use a 1D array to simulate:

int A[m*n];

Can someone explain why? Is it merely the double dereference you need
to do with the 2D array?

Sep 17 '07 #1
3 2078
dr*************@hotmail.com wrote:
instead of using 2D arrays:

int A[m][n];

I heard it was faster/preferred to use a 1D array to simulate:

int A[m*n];

Can someone explain why? Is it merely the double dereference you need
to do with the 2D array?
There is no "double dereference". A simple calculation is performed,
and if you simulate a 2D array, you will have to perform the offset
calculation instead of letting the compiler generate the code to do
that for you.

There is no real advantage _unless_ you can prove that whatever you
write is truly faster. In most cases it involves rewriting your
algorithm[s] to be a single loop instead of double, and providing
a custom addressing scheme for each.

Most often it's not worth the trouble, but sometimes it is. You
need to show that operations on your 2D arrays are really slow and
mostly due to [frequent] calculation of the element addresses. Do
not try to optimize what you don't have working yet.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 17 '07 #2
Victor Bazarov wrote:
dr*************@hotmail.com wrote:
>instead of using 2D arrays:

int A[m][n];

I heard it was faster/preferred to use a 1D array to simulate:

int A[m*n];

Can someone explain why? Is it merely the double dereference you need
to do with the 2D array?

There is no "double dereference". A simple calculation is performed,
and if you simulate a 2D array, you will have to perform the offset
calculation instead of letting the compiler generate the code to do
that for you.

There is no real advantage _unless_ you can prove that whatever you
write is truly faster. In most cases it involves rewriting your
algorithm[s] to be a single loop instead of double, and providing
a custom addressing scheme for each.

Most often it's not worth the trouble, but sometimes it is. You
need to show that operations on your 2D arrays are really slow and
mostly due to [frequent] calculation of the element addresses. Do
not try to optimize what you don't have working yet.

V
On a related note, spatial locality of successive array accesses can be
important here too. That is, for an array A[m][n], one may see
significantly better performance when performing the iteration over
successive n for each m, rather than vice versa. This amounts to
contiguous memory access in the underlying 1D array rather than making
large jumps.

Mark
Sep 17 '07 #3
On Sep 17, 5:39 pm, dragonslayer...@hotmail.com wrote:
instead of using 2D arrays:
int A[m][n];
I heard it was faster/preferred to use a 1D array to simulate:
int A[m*n];
Which is bullshit. The compiler will normally generate exactly
the same code for the first as that which you write for the
second.

The second may be faster if you iterator over the entire array,
because it would only require a single loop, rather than nested
loops. I imagine that most compilers would convert the nested
loops into a single loop, however, if optimization is turned on.

In any event, you have to measure for your compiler.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 18 '07 #4

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

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.