AA code 50x slower

Nathan S. no.public.email at example.com
Mon Feb 17 07:50:05 UTC 2020


On Sunday, 16 February 2020 at 12:57:43 UTC, AlphaPurned wrote:
> template AA(string[] S)
> {
> 	auto _do() { int[string] d; foreach(s; S) d[s] = 0; return d; }
> 	enum AA = _do;
> }
>
>
> if (t in AA!(["a", "and", "mp4", "mp3", "the", "with", "live", 
> "no", "&", "of", "band"])) continue;		
>
>
> The if statement literally causes a 50x slow down of the code. 
> (LDC debug, dmd debug takes about 1000 times longer)

template AA(string[] S) {
     __gshared int[string] AA;
     shared static this() {
     	int[string] d;
         foreach (s; S)
             d[s] = 0;
         AA = d;
     };
}
if (t in AA!(["a", "and", "mp4", "mp3", "the", "with", "live", 
"no", "&", "of", "band"])) continue;

This will have the performance you want if you don't care whether 
it works in CTFE.


More information about the Digitalmars-d-learn mailing list