34
How to clean an array

How to clean an array

  • Upload
    andysh

  • View
    2.657

  • Download
    5

Embed Size (px)

DESCRIPTION

Summary of how Moscow.pm people carry out "@a = ()" action.

Citation preview

How to clean an array

Perlish more-than-one-way

We hiredDelphi/SQL

programmer

as a Perl developer

Good programmerwrites good codein every language

But lots of fun

sub clear{   my $template = shift @args;   @{$self->{tokens}} = ();   foreach (map {$_->{name}} @{$template->{si}}){       while (my $r = shift @{$self->{$_}->{res}}){}   }}

Wat de fuck is dat?

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

sub clear{   my $template = shift @args;   @{$self->{tokens}} = ();   foreach (map {$_->{name}} @{$template->{si}}){       while (my $r = shift @{$self->{$_}->{res}}){}   }}

Note this

Posted that piece toMoscow.pm mailing list

And we bacame crazy

while (my $r = shift @a) {}

1

splice @a;

2

delete(@a[0..$#a]);

3

grep {shift @a} @a;

4

@a = ();

5

@a = ();

5

Boring!

undef @a;

6

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

while(defined(my $r = shift @a)){}

7

ccn@ccn-laptop ~$ perl -le 'my @a=(undef,1,2,3);while(defined(my $r=shift @a)){};print @a'123

Doesn’t work

Let’s make it useful

$str = "abc\c{0}def";@a = split //, $str;$c++ while(shift @a);say $c - 1;

Calculate zero-ending string length

while(chomp $str) {};

8

while(@a) {shift @a}

9

shift @a while @a;

10

Much better!

shift @a while @a and die;

11

shift @a while scalar @a;

12

$#a = -1

13

Who remembers?!

my @a = qw(oh eh); while (push(@a, shift @a)) {};

Endless sex loop

Bonus

use Benchmark qw(:all :hireswallclock);my $size = 1000;cmpthese timethese -3, {   '@a = ...'          => sub { my @a = (('test')x$size);       return; },   'undef @a'          => sub { my @a = (('test')x$size); undef @a;       return; },   '@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 @awhile @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%undef @a          4745/s               80%               41%        0%     --   -1%      -2%      -2%@a=()             4781/s               81%               42%        1%     1%    --      -1%      -1%$#a = -1          4834/s               83%               43%        2%     2%    1%       --      -0%@a = ...          4834/s               83%               43%        2%     2%    1%       0%       --

при $size = 100;shift @a while @a 25428/s                --              -27%     -43%    -45%     -46%  -47%     -47%delete @a[0..#$a] 34689/s               36%                --     -23%    -25%     -27%  -27%     -28%$#a = -1          44823/s               76%               29%       --     -4%      -5%   -6%      -6%splice @a         46547/s               83%               34%       4%      --      -2%   -2%      -3%undef @a          47424/s               87%               37%       6%      2%       --   -1%      -1%@a=()             47669/s               87%               37%       6%      2%       1%    --      -0%@a = ...          47908/s               88%               38%       7%      3%       1%    1%       --

при $size=10;shift @a while @a 206027/s                --              -20%-32%      -40%  -42%     -42%     -45%delete @a[0..#$a] 257112/s               25%                ---15%      -26%  -28%     -28%     -31%$#a = -1          302268/s               47%               18%--      -13%  -15%     -15%     -19%splice @a         345669/s               68%               34%14%        --   -3%      -3%      -8%@a=()             355783/s               73%               38%18%        3%    --      -0%      -5%undef @a          356498/s               73%               39%18%        3%    0%       --      -5%@a = ...          374747/s               82%               46%

Benchmarked

TIMTOWTDI or die;

Andrew Shitov

talks.shitov.ru | [email protected]