Cost of assoc array?

2014-05-14 Thread Chris via Digitalmars-d-learn
I have code that uses the following: string[][size_t] myArray; 1. myArray = [0:[t, o, m], 1:[s, m, i, th]]; However, I've found out that I never need an assoc array and a linear array would be just fine, as in 2. myArray = [[t, o, m], [s, m, i, th]]; Is there any huge difference as regards

Re: Cost of assoc array?

2014-05-14 Thread bearophile via Digitalmars-d-learn
Chris: Is there any huge difference as regards performance and memory footprint between the two? Or is 2. basically 1. under the hood? An associative array is a rather more complex data structure, so if you don't need it, use something simpler. There is difference in both the amount of

Re: Cost of assoc array?

2014-05-14 Thread Chris via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 10:20:51 UTC, bearophile wrote: Chris: Is there any huge difference as regards performance and memory footprint between the two? Or is 2. basically 1. under the hood? An associative array is a rather more complex data structure, so if you don't need it, use

Re: Cost of assoc array?

2014-05-14 Thread dennis luehring via Digitalmars-d-learn
Am 14.05.2014 12:33, schrieb Chris: On Wednesday, 14 May 2014 at 10:20:51 UTC, bearophile wrote: Chris: Is there any huge difference as regards performance and memory footprint between the two? Or is 2. basically 1. under the hood? An associative array is a rather more complex data

Re: Cost of assoc array?

2014-05-14 Thread bearophile via Digitalmars-d-learn
Chris: Do you mean the difference is negligible in many cases? Yes, but you have to profile the code (or reason about it well, with knowledge of the data structures and their usage patterns) if you want to know what your case is. Bye, bearophile

Re: Cost of assoc array?

2014-05-14 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 09:38:10 UTC, Chris wrote: I have code that uses the following: string[][size_t] myArray; 1. myArray = [0:[t, o, m], 1:[s, m, i, th]]; However, I've found out that I never need an assoc array and a linear array would be just fine, as in 2. myArray = [[t, o, m],

Re: Cost of assoc array?

2014-05-14 Thread Chris via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 11:13:10 UTC, John Colvin wrote: On Wednesday, 14 May 2014 at 09:38:10 UTC, Chris wrote: I have code that uses the following: string[][size_t] myArray; 1. myArray = [0:[t, o, m], 1:[s, m, i, th]]; However, I've found out that I never need an assoc array and a

Re: Cost of assoc array?

2014-05-14 Thread dennis luehring via Digitalmars-d-learn
Am 14.05.2014 15:20, schrieb Chris: Profiling is not really feasible, because for this to work properly, I would have to introduce the change first to be able to compare both. Nothing worse than carefully changing things only to find out, it doesn't really speed up things. why not using an

Re: Cost of assoc array?

2014-05-14 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 13:20:40 UTC, Chris wrote: On Wednesday, 14 May 2014 at 11:13:10 UTC, John Colvin wrote: On Wednesday, 14 May 2014 at 09:38:10 UTC, Chris wrote: I have code that uses the following: string[][size_t] myArray; 1. myArray = [0:[t, o, m], 1:[s, m, i, th]]; However,

Re: Cost of assoc array?

2014-05-14 Thread Chris via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 13:31:53 UTC, dennis luehring wrote: Am 14.05.2014 15:20, schrieb Chris: Profiling is not really feasible, because for this to work properly, I would have to introduce the change first to be able to compare both. Nothing worse than carefully changing things only to

Re: Cost of assoc array?

2014-05-14 Thread bearophile via Digitalmars-d-learn
Chris: foreach (size_t i; 0..myArray.length) { // do something with myArray[i]; } There are various better ways to use a foreach on an array: foreach (immutable x; myArray) { foreach (ref const x; myArray) { foreach (ref x; myArray) { Bye, bearophile

Re: Cost of assoc array?

2014-05-14 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 13:49:22 UTC, bearophile wrote: Chris: foreach (size_t i; 0..myArray.length) { // do something with myArray[i]; } There are various better ways to use a foreach on an array: foreach (immutable x; myArray) { foreach (ref const x; myArray) { foreach (ref x;

Re: Cost of assoc array?

2014-05-14 Thread Chris via Digitalmars-d-learn
On Wednesday, 14 May 2014 at 13:44:40 UTC, John Colvin wrote: Yes, they are much faster. Normal array indexing is equivalent to *(myArray.ptr + index) plus an optional bounds check, whereas associative array indexing is a much, much larger job. Why were you using associative arrays in the