Reserving/Preallocating associative array?

bearophile bearophileHUGS at lycos.com
Tue Dec 24 15:13:58 PST 2013


> Some ides to speed up your code:
> - Try to use parse instead of split + to!size_t
> - Try to use byLineFast, in the attach here (the code is not 
> mine): https://d.puremagic.com/issues/attachment.cgi?id=1305
> - Try to disable the GC before the associative array creation 
> and re-enable it when it's built. (This could double your max 
> memory usage or more). To disable it import GC from core.memory 
> and call GC.disable; and GC.enable;


This code is almost 2.5X faster on my PC (but I am using only 
300_000 lines of text):

void main() {
     import std.stdio, std.conv, std.string, core.memory;
     import bylinefast;

     GC.disable;
     size_t[size_t] unions;
     foreach (line; "input.txt".File.byLineFast) {
         line.munch(" \t"); // skip ws
         immutable i = line.parse!size_t;
         line.munch(" \t"); // skip ws
         immutable j = line.parse!size_t;
         unions[i] = j;
     }
     GC.enable;
}


Bye,
bearophile


More information about the Digitalmars-d mailing list