Fast switching..

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Jun 12 06:19:48 PDT 2008


"janderson" <askme at me.com> wrote in message 
news:g2q2bp$13ef$3 at digitalmars.com...
>
>
> I thought in a discussion earlier with Walter (a few years back) that DMD 
> would generate a string hash and then jump to that hash when you use a 
> string as the input.  In many cases the branching will be the expensive 
> part so I guess a hash is pretty close to optimal performance for the 
> general case.  Although with optimisation you can always make things run 
> faster.
>
> -Joel

Guessing won't be necessary :)  The implementation of string switches is in 
dmd/src/phobos/internal/switch.d.  The compiler puts the case names into a 
sorted array of strings, and the string switch just does a binary search on 
it.  It's somewhat optimized though -- it won't actually do a string 
comparison unless the strings are of the same length and their first 
character is the same. 





More information about the Digitalmars-d mailing list