Reserving/Preallocating associative array?

Gordon me at home.com
Wed Dec 25 08:29:01 PST 2013


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.


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).

Many thanks,
  -gordon


More information about the Digitalmars-d mailing list