in reply to Optimize debugging subs

Your more elegant equivalent achives the same leval of optimization, print $_[0] if ENABLE_DEBUG; evaluates to false.

Replies are listed 'Best First'.
Re^2: Optimize debugging subs
by danb (Friar) on Apr 05, 2006 at 04:20 UTC
    This benchmark indicates that you are wrong.
    sub ENABLE_DEBUG () { 0 } sub debug_inelegant { print $_[0]; } sub debug_elegant { print $_[0] if ENABLE_DEBUG; } use Benchmark qw(:all) ; cmpthese( 30000000, { 'inelegant' => "debug_inelegant 'test' if ENABLE_DEBUG;", 'elegant' => "debug_elegant 'test';" }, );
    Output:
    Rate elegant inelegant elegant 3337041/s -- -95% inelegant 62500000/s 1773% --

    --Dan

      According to your benchmark, elegant takes 0.0000003 seconds (0.3 microseconds) to execute. What's the problem?

        Good point; what about this?

        Lots debug() calls inside a long-running loop, inside many very frequently called subs, running in a virtual server program (with access to only 10% of the CPU), running on a commodity server (Pentium-III 1.3 Ghz), being accessed by many simultaneous clients.

        That's a lot different than the Benchmark I just ran on a Athlon64 X2 2.4ghz.

        Besides, what about the enjoyment of knowing that there is *no* debugging code in your program?

        --Dan