January 19, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to alb | On Tuesday, 19 January 2016 at 19:14:30 UTC, alb wrote: > So guys: Ali, Mike Parker and tsbockman thanks for all your explanation, in fact looking now I and after making some tests I really got it. > > So: > int[2] a1; // Array of 2 elements of type int > > int[2][5] a2; // Array of 2 elements of type int divided in 5 rows > > writeln(a2[0]); // = accessing row 0 = [0,0] > writeln(a2[4]); // = accessing row 4 = [0,0] One other thing you may want to keep in mind when working on this kind of thing - when you loop over a multi-dimensional array, the order matters. For large arrays, this: int[c_max][r_max] arr; foreach(r; 0 .. r_max) { foreach(c; 0 .. c_max) { // do something with arr[r][c] here } } Can be *much* faster than this: int[c_max][r_max] arr; foreach(c; 0 .. c_max) { foreach(r; 0 .. r_max) { // do something with arr[r][c] here } } The reason is that the first version access the elements in the order that they are actually stored in memory, whereas the second forces the CPU to jump between rows for each element. > If that in mind, now it all makes sense for me, and of course it's consistent as well. Sorry to bother about this, but I think this will help other newcomers. > > Thanks again for help/tips which helped turn my mindset. > > Albert. You're welcome. And yes, it can definitely be confusing. I understand why the array syntax in D is the way it is, but that still doesn't always save me from mixing things up once in a while anyway. |
January 21, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to tsbockman | On Tuesday, 19 January 2016 at 20:39:37 UTC, tsbockman wrote:
> On Tuesday, 19 January 2016 at 19:14:30 UTC, alb wrote:
>> [...]
>
> One other thing you may want to keep in mind when working on this kind of thing - when you loop over a multi-dimensional array, the order matters.
>
> For large arrays, this:
>
> int[c_max][r_max] arr;
>
> foreach(r; 0 .. r_max)
> {
> foreach(c; 0 .. c_max)
> {
> // do something with arr[r][c] here
> }
> }
>
> Can be *much* faster than this:
>
> int[c_max][r_max] arr;
>
> foreach(c; 0 .. c_max)
> {
> foreach(r; 0 .. r_max)
> {
> // do something with arr[r][c] here
> }
> }
>
> The reason is that the first version access the elements in the order that they are actually stored in memory, whereas the second forces the CPU to jump between rows for each element.
>
>> [...]
>
> You're welcome.
>
> And yes, it can definitely be confusing. I understand why the array syntax in D is the way it is, but that still doesn't always save me from mixing things up once in a while anyway.
I don't remember where I saw it, but actually, in static multi-dimensional arrays, arr[0][1] is next to arr[0][0] in memory. You can see it with:
void main () {
ubyte [7][5] arr;
import std.stdio : writeln;
writeln ( & arr[0][0], " ", & arr[0][1], " ", & arr [1][0] );
}
|
January 21, 2016 Re: Doubt - Static multidimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nemo | On Thursday, 21 January 2016 at 01:36:21 UTC, Nemo wrote: > I don't remember where I saw it, but actually, in static multi-dimensional arrays, arr[0][1] is next to arr[0][0] in memory. You can see it with: > > void main () { > ubyte [7][5] arr; > import std.stdio : writeln; > writeln ( & arr[0][0], " ", & arr[0][1], " ", & arr [1][0] ); > } Yes. That's consistent both with what I wrote in the message that you quoted, and with how non-static arrays work. dpaste: http://dpaste.dzfl.pl/5ca02bd98f82 |
Copyright © 1999-2021 by the D Language Foundation