Greetings,
for an array byte[3][3] myArr, I can code myArr[0] = 5 and have:
5,5,5
0,0,0
0,0,0
Can I perform a similar assignment to the column? This, myArr[][0] = 5, doesn't work.
Thanks!
Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 31 Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Greetings, for an array byte[3][3] myArr, I can code myArr[0] = 5 and have: Can I perform a similar assignment to the column? This, myArr[][0] = 5, doesn't work. Thanks! |
February 01 Re: Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Tuesday, 31 January 2023 at 01:04:41 UTC, Paul wrote: >Can I perform a similar assignment to the column? This, myArr[][0] = 5, doesn't work. Of course, this question has a short answer and a long answer. So the issue is more about column-major. I am someone who likes to talk with codes. In fact, everything is side by side in memory. This example (something like array) covers the issue:
SDB@79 |
February 01 Re: Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Posted in reply to Salih Dincer | Sorry, I forget this:
SDB@79 |
February 01 Re: Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Posted in reply to Salih Dincer | On Wednesday, 1 February 2023 at 03:45:11 UTC, Salih Dincer wrote: >On Tuesday, 31 January 2023 at 01:04:41 UTC, Paul wrote: >Can I perform a similar assignment to the column? This, myArr[][0] = 5, doesn't work. Of course, this question has a short answer and a long answer. So the issue is more about column-major. I am someone who likes to talk with codes. In fact, everything is side by side in memory. This example (something like array) covers the issue: Thanks Salih. Much appreciated. |
February 01 Re: Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Tuesday, 31 January 2023 at 01:04:41 UTC, Paul wrote: >Greetings, for an array byte[3][3] myArr, I can code myArr[0] = 5 and have: Can I perform a similar assignment to the column? This, myArr[][0] = 5, doesn't work. This works fine for small arrays, but for large arrays such access pattern is cache unfriendly. It's usually best to redesign the code to avoid assignments to columns if possible (for example, by working with a transposed array). The language is not providing a convenient shortcut for something that is usually undesirable and expensive. And I think that this is actually reasonable. |
February 01 Re: Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Posted in reply to Siarhei Siamashka | On Wednesday, 1 February 2023 at 13:14:47 UTC, Siarhei Siamashka wrote: >On Tuesday, 31 January 2023 at 01:04:41 UTC, Paul wrote: >Greetings, for an array byte[3][3] myArr, I can code myArr[0] = 5 and have: Can I perform a similar assignment to the column? This, myArr[][0] = 5, doesn't work. This works fine for small arrays, but for large arrays such access pattern is cache unfriendly. It's usually best to redesign the code to avoid assignments to columns if possible (for example, by working with a transposed array). The language is not providing a convenient shortcut for something that is usually undesirable and expensive. And I think that this is actually reasonable. If the code is slow, then profile and try to speed up parts that need it. The slowness may be due to a problem like this, but not always. The OP could also try mir's slices.
|
February 02 Re: Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Wednesday, 1 February 2023 at 05:51:31 UTC, Paul wrote: >Thanks Salih. Much appreciated. It's my pleasure to solve this problem... I have struck upon an idea that would not affect (i think) the speed of the processor requesting memory from the heap on a page-by-page basis but limited 😀
> row 0 => 32767 41149 49531 57913: 16301273062966132735 @SDB79 |
February 02 Re: Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Tuesday, 31 January 2023 at 01:04:41 UTC, Paul wrote: >Greetings, for an array byte[3][3] myArr, I can code myArr[0] = 5 and have: Can I perform a similar assignment to the column? This, myArr[][0] = 5, doesn't work. Thanks! Here's a solution using standard-library functions:
The only tricky part here is the call to Once we've done that, By the way, if we use Godbolt to look at the generated code, we can see that LDC with optimizations enabled compiles this very efficiently--it is able to inline all the range functions and unroll the loop: |
February 02 Re: Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Thursday, 2 February 2023 at 05:47:34 UTC, Paul Backus wrote: >Here's a solution using standard-library functions:
Thank you for your contribution. In this way, we can find the opportunity to compare. It can be seen below that the structure (LimitedArray) implemented with union is at least 15 times and at most 20 times faster.
SDB@79 |
February 02 Re: Assign to Array Column | ||||
---|---|---|---|---|
| ||||
Posted in reply to Salih Dincer | On Thursday, 2 February 2023 at 10:47:19 UTC, Salih Dincer wrote: >It can be seen below that the structure (LimitedArray) implemented with Except that it isn't. How did you manage to get such ridiculous results? Even running your program compiled by LDC with optimizations gives me the following: #line1 31 µs This isn't a proper way to do benchmarks. |