Leser: 30
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/perl my %case=( a => sub{ return 1*shift; }, b => sub{ return 2*shift; }, c => sub{ return 3*shift; }, d => sub{ return 4*shift; }, ); sub foo{ goto $case{chr(rand(4)+97)}; } print foo(55)."\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my %case=( a => sub{ return 1*shift; }, b => sub{ return 2*shift; }, c => sub{ return 3*shift; }, d => sub{ return 4*shift; }, ); *my_a=$case{a}; *my_b=$case{b}; *my_c=$case{c}; *my_d=$case{d}; sub rstr{ return chr(int(rand(4))+97) } sub gto { goto $case{rstr()}; } sub rcl { $case{rstr()}->(@_); } sub als { my $f=rstr(); $f eq 'a' and return my_a(@_); $f eq 'b' and return my_b(@_); $f eq 'c' and return my_c(@_); $f eq 'd' and return my_d(@_); } cmpthese(1000000, { 'goto' => sub{ gto(55); }, 'refcall' => sub{ rcl(55); }, 'alias' => sub{ als(55); }, });
1
2
3
4
Rate alias refcall goto
alias 414938/s -- -9% -15%
refcall 454545/s 10% -- -7%
goto 487805/s 18% 7% --
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my %case=( a => sub{ return 1*shift; }, b => sub{ return 2*shift; }, c => sub{ return 3*shift; }, d => sub{ return 4*shift; }, ); *my_a=$case{a}; *my_b=$case{b}; *my_c=$case{c}; *my_d=$case{d}; sub rstr{ return chr(int(rand(4))+97) } sub gto{ goto $case{rstr()}; } sub rcl{ $case{rstr()}->(@_); } sub scl{ &{ $case{rstr()} }; } sub als { my $f=rstr(); $f eq 'a' and return my_a(@_); $f eq 'b' and return my_b(@_); $f eq 'c' and return my_c(@_); $f eq 'd' and return my_d(@_); } cmpthese(1000000, { goto => sub{ gto(55); }, refcall => sub{ rcl(55); }, alias => sub{ als(55); }, subcall => sub{ scl(55); }, });
1
2
3
4
5
Rate alias refcall goto subcall
alias 400000/s -- -11% -16% -20%
refcall 450450/s 13% -- -5% -10%
goto 473934/s 18% 5% -- -6%
subcall 502513/s 26% 12% 6% --