Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; use Benchmark 'cmpthese'; cmpthese -1, { map { $_ => \&$_ } 'a' .. 'g' }; sub a {eval 1 for 0 .. 999} {use feature ':5.10'; sub b {eval 1 for 0 .. 999}} {use 5.010; sub c {eval 1 for 0 .. 999}} {no feature ':all'; use feature ':5.10'; sub d {eval 1 for 0 .. 999}} {use feature 'say'; sub e {eval 1 for 0 .. 999}} { no feature 'say'; sub f {eval 1 for 0 .. 999}} {use feature 'say'; no feature 'say'; sub g {eval 1 for 0 .. 999}}

Results:

Rate d b e g f a c d 119/s -- -0% -18% -23% -25% -53% -53% b 119/s 0% -- -18% -23% -25% -53% -53% e 145/s 22% 22% -- -7% -8% -43% -43% g 155/s 30% 30% 7% -- -2% -38% -38% f 158/s 33% 33% 9% 2% -- -37% -37% a 252/s 112% 112% 74% 62% 59% -- -0% c 252/s 112% 112% 74% 63% 59% 0% --

Looks like feature pragma leaves some nasty attribute somewhere if mentioned. String eval is slow as it is already, why make it even slower. Well, partly tongue-in-cheek useless entertainment, but still partly: I used to declare like "c" case, then made a habit of explicitly declaring like e.g. "e"; now what? perfectionist in me urges to return to habits of years long gone. The "d" is there because feature says it should be exactly the same as "c".

Replies are listed 'Best First'.
Re: A plague on all your features
by tobyink (Canon) on Mar 06, 2023 at 14:19 UTC

    Interesting. You can maybe figure out why 'c' and 'd' differ so much using this:

    use strict; use warnings; no strict 'refs'; use Data::Dumper; $Data::Dumper::Sortkeys = 1; $Data::Dumper::Deparse = 1; print Dumper( { map { $_ => $_->() } 'a' .. 'g' } ); sub dumphints { my @caller = caller(0); $caller[10]; } sub a {dumphints()} {use feature ':5.10'; sub b {dumphints()}} {use 5.010; sub c {dumphints()}} {no feature ':all'; use feature ':5.10'; sub d {dumphints()}} {use feature 'say'; sub e {dumphints()}} { no feature 'say'; sub f {dumphints()}} {use feature 'say'; no feature 'say'; sub g {dumphints()}}

    I find the output for 'c' honestly surprising.

      $VAR1 = { 'a' => undef, 'b' => { 'feature_bareword_filehandles' => 1, 'feature_indirect' => 1, 'feature_multidimensional' => 1, 'feature_say' => 1, 'feature_state' => 1, 'feature_switch' => 1 }, 'c' => undef, 'd' => { 'feature_bareword_filehandles' => 1, 'feature_indirect' => 1, 'feature_multidimensional' => 1, 'feature_say' => 1, 'feature_state' => 1, 'feature_switch' => 1 }, 'e' => { 'feature_bareword_filehandles' => 1, 'feature_indirect' => 1, 'feature_multidimensional' => 1, 'feature_say' => 1 }, 'f' => { 'feature_bareword_filehandles' => 1, 'feature_indirect' => 1, 'feature_multidimensional' => 1 }, 'g' => { 'feature_bareword_filehandles' => 1, 'feature_indirect' => 1, 'feature_multidimensional' => 1 } };

        I don't know why the hints are undef for c, but I can confirm it's accurate.

        $ diff -u \ <( perl -MO=Concise,b b.pl 2>&1 ) \ <( perl -MO=Concise,c b.pl 2>&1 ) --- /dev/fd/63 2023-03-06 20:59:39.932888013 -0400 +++ /dev/fd/62 2023-03-06 20:59:39.932888013 -0400 @@ -1,8 +1,8 @@ b.pl syntax OK -main::b: +main::c: 5 <1> leavesub[1 ref] K/REFC,1 ->(end) - <@> lineseq KP ->5 -1 <;> nextstate(main 1252 b.pl:15) v:%,*,&,x*,x&,x$,fea=15 ->2 +1 <;> nextstate(main 1257 b.pl:16) v:*,&,x*,x&,x$,fea=1 ->2 4 <1> entersub KS ->5 - <1> ex-list K ->4 2 <0> pushmark s ->3

        Note the different value of the hints (fea= for "feature") for the two lines.

Re: A plague on all your features
by ikegami (Patriarch) on Mar 07, 2023 at 00:40 UTC

    Some observations:

    While the differences look huge in that chart, it shows a difference of 4.4 μs between the fastest and the slowest. (2.3 μs on my machine.)

    Using 5.36, I consistently obtain similar results (b=d > e > f=g > a=c)

Re: A plague on all your features
by Leon Timmermans (Acolyte) on Mar 07, 2023 at 18:40 UTC
    Yeah, there's an optimization in perl that stores feature bundles in $^H instead of %^H, as you can see it is an effective optimization. Though in practice it shouldn't really matter much in most cases, this is a fairly contrived example.