http://qs1969.pair.com?node_id=28710


in reply to RE (tilly) 1: Expand your world
in thread Expand your world

I have trouble with your assertion that this will be more efficient. Any gain in perfomance by using a hash instead of an if-else clause is going to be offset by the multiple eval()s. I also contend that it is just as easy to add another elsif as to add another key-value pair to the hash. Anyway, that's just my opinion.

Replies are listed 'Best First'.
R (tilly) 3: Expand your world
by tilly (Archbishop) on Aug 20, 2000 at 11:30 UTC
    If you have a long list of arguments, it will be.

    First of all evals don't cost much more than just having had that much code in the first place. Secondly all of the evals take place up front. So it is just like having a longer program.

    But at runtime I have a hash lookup (constant time no matter how large the hash is) rather than repeated tests, So I have traded compile time away for faster run-time behaviour.

    FWIW I first realized the importance of this win when trying to speed up a program that categorized states into ACLI regions for a huge amount of text information. Just moving the logic out of if/elsif/elsif/else type constructs into hash lookups and/or smart REs was an order of magnitude improvement.

    Cheers, Ben