there's more than one way to empty it

34
There’s more than one way to empty it

Upload: andysh

Post on 18-Nov-2014

2.077 views

Category:

Technology


2 download

DESCRIPTION

Funny story of how Perl people can clear an array.

TRANSCRIPT

Page 1: There's more than one way to empty it

There’s more

than one way to empty it

Page 2: There's more than one way to empty it

Perlish more-than-one-way

Page 3: There's more than one way to empty it

We hiredDelphi/SQL

programmer

Page 4: There's more than one way to empty it

as a Perl developer

Page 5: There's more than one way to empty it

Good programmerwrites good codein every language

Page 6: There's more than one way to empty it

But lots of fun

Page 7: There's more than one way to empty it

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

Page 8: There's more than one way to empty it

Che cazzo di..?*

* WTF? (Italian)

Page 9: There's more than one way to empty it

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

Page 10: There's more than one way to empty it

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

Note this

Page 11: There's more than one way to empty it

Posted that piece toMoscow.pm mailing list

Page 12: There's more than one way to empty it

And we bacame crazy

Page 13: There's more than one way to empty it

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

1

Page 14: There's more than one way to empty it

splice @a;

2

Page 15: There's more than one way to empty it

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

3

Page 16: There's more than one way to empty it

grep {shift @a} @a;

4

Page 17: There's more than one way to empty it

@a = ();

5

Page 18: There's more than one way to empty it

@a = ();

5

Boring!

Page 19: There's more than one way to empty it

undef @a;

6

Page 20: There's more than one way to empty it

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

Page 21: There's more than one way to empty it

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

7

Page 22: There's more than one way to empty it

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

Page 23: There's more than one way to empty it

Doesn’t work

Page 24: There's more than one way to empty it

Let’s make it useful

Page 25: There's more than one way to empty it

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

Calculate zero-ending string length

Page 26: There's more than one way to empty it

while(chomp $str) {};

8

Page 27: There's more than one way to empty it

while(@a) {shift @a}

9

Page 28: There's more than one way to empty it

shift @a while @a;

10

Much better!

Page 29: There's more than one way to empty it

shift @a while @a and die;

11

Page 30: There's more than one way to empty it

shift @a while scalar @a;

12

Page 31: There's more than one way to empty it

$#a = -1

13

Who remembers?!

Page 32: There's more than one way to empty it

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

Page 33: There's more than one way to empty it

TIMTOWTDI or die;

Andrew Shitov

talks.shitov.ru | [email protected]

Page 34: There's more than one way to empty it