<?xml version="1.0" encoding="windows-1252"?>
<node id="629753" title="Re^3: Nested evals - are they evil? (sample benchmarks)" created="2007-07-31 03:05:17" updated="2007-07-30 23:05:17">
<type id="11">
note</type>
<author id="61104">
cLive ;-)</author>
<data>
<field name="doctext">
Hmmm, well, I stripped it down further:
&lt;code&gt;use strict;
use warnings;
use Benchmark 'cmpthese';

cmpthese(-1, {
    'eval'   =&gt; \&amp;eval_code,
    'noeval' =&gt; \&amp;no_eval_code,
 });

 sub eval_code {
     eval {
        some_more_code ();
     };
     if ($@) {
         error_sub();
     }
}

sub no_eval_code {
    if (0) {
        error_sub();
    }
    else {
        some_more_code ();
    }
}

sub some_more_code { }
sub error_sub { }&lt;/code&gt;
And got this (I think the effectively empty eval is meaningless - see here where I ran it 4 times):
&lt;code&gt;            Rate   eval noeval
eval   1298354/s     --   -37%
noeval 2066450/s    59%     --

            Rate   eval noeval
eval   1316991/s     --   -38%
noeval 2123851/s    61%     --

            Rate   eval noeval
eval   1217925/s     --   -21%
noeval 1534287/s    26%     --

            Rate   eval noeval
eval   1298354/s     --   -41%
noeval 2205537/s    70%     --
&lt;/code&gt;
I have a feeling an empty eval gets optimized away at compile time (though I am most probably wrong :)&lt;p&gt;How about if I add in an increment do ensure each eval has to be 'eval'd, ie
&lt;code&gt;
use strict;
use warnings;
use Benchmark 'cmpthese';

my $x;

cmpthese(-1, {
    'eval'   =&gt; \&amp;eval_code,
    'noeval' =&gt; \&amp;no_eval_code,
 });

sub eval_code {
     $x=1;
     eval {
        some_more_code ();
     };
     if ($@) {
         error_sub();
     }
}

sub no_eval_code {
    if ($x==0) {
        error_sub();
    }
    else {
        $x=0;
        some_more_code ();
    }
}

sub some_more_code { $x++ }
sub error_sub { }

&lt;/code&gt;
I get this:
&lt;code&gt;            Rate   eval noeval
eval    989400/s     --   -23%
noeval 1286220/s    30%     --
-bash-3.00$ perl tmp.pl
            Rate   eval noeval
eval   1092266/s     --   -21%
noeval 1379705/s    26%     --

            Rate   eval noeval
eval   1069351/s     --   -19%
noeval 1327443/s    24%     --

            Rate   eval noeval
eval   1071850/s     --   -25%
noeval 1435550/s    34%     --

            Rate   eval noeval
eval   1071850/s     --   -26%
noeval 1456355/s    36%     --
&lt;/code&gt;
Either way, I still haven't been able to create a counter example that shows eval to be faster.&lt;p&gt;
But anyway, considering the number of runs a second, I have a feeling my energies are probably better spent focusing on other parts of the code as far as optimizations are concerned :)</field>
<field name="root_node">
629680</field>
<field name="parent_node">
629736</field>
</data>
</node>
