codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#!/usr/bin/perl use strict; use warnings; use Benchmark; my $key = "foo"; my %types = ( "a full" => { A => { B => { foo => "foo" } } }, "b no_top" => { A => { B => { } } }, "c only_a" => { A => { } }, "d none" => { }, ); my $ref; my %subs = ( insane => sub { return exists ${ ${ ${ $ref || {} }{A} || {} }{B} || {} }{$key} }, sane => sub { return ( exists $ref->{A} and exists $ref->{A}{B} and exists $ref->{A}{B}{$key} ); }, sane_with_high_precedence => sub { return ( exists $ref->{A} && exists $ref->{A}{B} && exists $ref->{A}{B}{$key} ); }, ); for my $type (keys %types) { $ref = $types{$type}; print "$type\n"; for my $sub (keys %subs) { print "\t$sub: ", $subs{$sub}() ? "true" : "false" , "\n"; } } for my $type (sort keys %types) { $ref = $types{$type}; print "\n$type\n"; Benchmark::cmpthese -1, \%subs; }
Private
[
?
]
Run code
Submit