Mais conteúdo relacionado Semelhante a There's more than one way to empty it (20) Mais de Andrew Shitov (20) There's more than one way to empty it7. sub clear{
my $template = shift @args;
@{$self->{tokens}} = ();
foreach (map {$_->{name}} @{$template->{si}}){
while (my $r = shift @{$self->{$_}->{res}}){}
}
}
9. sub clear{
my $template = shift @args;
@{$self->{tokens}} = ();
foreach (map {$_->{name}} @{$template->{si}}){
while (my $r = shift @{$self->{$_}->{res}}){}
}
}
A subroutine to make an object empty
10. sub clear{
my $template = shift @args;
@{$self->{tokens}} = ();
foreach (map {$_->{name}} @{$template->{si}}){
while (my $r = shift @{$self->{$_}->{res}}){}
}
}
Note this
20. ccn@ccn-laptop ~
$ perl -le 'my @a=(1,2,3); while(my $r =
shift @a){}; print @a'
ccn@ccn-laptop ~
$ perl -le 'my @a=(0,1,2,3); while(my $r
= shift @a){}; print @a'
123
25. $str = "abcc{0}def";
@a = split //, $str;
$c++ while(shift @a);
say $c - 1;
Calculate zero-ending string length
32. use Benchmark qw(:all :hireswallclock);
my $size = 1000; Be
cmpthese timethese -3, { nc
'@a = ...' => sub { my @a = (('test')x$size); h m
return; }, ar
'undef @a' => sub { my @a = (('test')x$size); undef @a; ke
return; }, d
'@a=()' => sub { my @a = (('test')x$size); @a = ();
return; },
'splice @a' => sub { my @a = (('test')x$size); splice @a;
return; },
'$#a = -1' => sub { my @a = (('test')x$size); $#a = -1;
return; },
'shift @a while @a' => sub { my @a = (('test')x$size); shift @a
while @a; return; },
'delete @a[0..#$a]' => sub { my @a = (('test')x$size);
delete(@a[0..$#a]); return; },
};
при $size=1000;
shift @a while @a 2642/s -- -22% -44%
-44% -45% -45% -45%
delete @a[0..#$a] 3376/s 28% -- -28%
-29% -29% -30% -30%
splice @a 4722/s 79% 40% --
-0% -1% -2% -2%