Random D geekout

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Apr 21 10:55:45 PDT 2012


On Sat, Apr 21, 2012 at 06:17:58PM +0200, nhk wrote:
> Please bear with my ignorance I'm new to D, but why is that any
> better compared to a simple
> 
> switch(key){
> default: throw Exception("Invalid attribute '%s'".format(key));
> case "name": d.name = value;
>                break;
> ...
> ...
> }

Firstly, the current version of dmd doesn't do any special translation
of a switch statement on a string variable, so it just becomes a
sequence of if-statements with string comparisons (slow). Using an
associative array makes the lookup O(1), which is fast(er) when you have
a lot of keys.

Second, using an AA allowed me to factor out the code that does the
parsing (see the parseBlock function in the second version of my code).
There's no way to do this with a switch statement.

In retrospect, the second point is probably more important, because the
use of delegates probably delays the point at which AA performance
starts to overtake a switch statement.


T

-- 
Caffeine underflow. Brain dumped.


More information about the Digitalmars-d mailing list