Reserving/Preallocating associative array?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Dec 25 08:47:02 PST 2013


On 12/25/13 8:29 AM, Gordon wrote:
> On Tuesday, 24 December 2013 at 23:52:49 UTC, Andrei Alexandrescu wrote:
>>
>> void main()
>> {
>>     size_t[size_t] unions;
>>     foreach (e; "input.txt"
>>             .slurp!(size_t, size_t)("%s %s").sort.uniq ) {
>>         unions[e[0]] = e[1];
>>     }
>> }
>>
>
> Thanks for this very elegant solution!
>
> For completeness (since we're dealing with timing):
>
> 1. Running the above code with Garbage-collection enabled, takes 1m45s.
>
> 2. Running it with GC disabled takes 50s .
>
> 3. Running with GC disabled and without sort+uniq (my data is already
> uniq'd), takes 40s.

Thanks for the numbers!

> This is a beautiful idiomatic D code, but for now I think I'll stick
> with the more verbose code above.
>
> Ine reason is that I want to provide helpful and verbose error messages
> for invalid input (e.g. "Invalid numeric value found in field 3 line
> 1000") - and with the "slurp" method, any input error will result in a
> not-so-helpful exception (e.g.
> "std.conv.ConvException@/usr/include/dmd/phobos/std/conv.d(2009):
> Unexpected 'H' when converting from type char[] to type ulong).

Thanks, I added https://d.puremagic.com/issues/show_bug.cgi?id=11816 to 
keep track of this.


Andrei




More information about the Digitalmars-d mailing list