in reply to Fastest way to lookup a point in a set

Hello eyepopslikeamosquito,

From testing using Perl 5.18.2, join(':', @$p) runs faster than $p->[0] .':'. $p->[1]. Although, it runs only slightly faster on newer Perl releases.

sub str_look { my $cells = shift; for my $p (@points) { # my $h = $p->[0] . ':' . $p->[1]; # exists $cells->{$p->[0] . ':' . $p->[1]} or die; exists $cells->{ join(':', @$p) } or die; } exists $cells->{'notfound'} and die; exists $cells->{'notfound2'} and die; exists $cells->{'notfound3'} and die; }

Results:

str_look concat 34843.21/s str_look join 42643.92/s

Update: Added st2_look including mat_hash by kcott.

... sub st2_look { my $cells = shift; for my $p (@points) { exists $cells->{ join(':',@$p) } or die; } exists $cells->{'notfound'} and die; exists $cells->{'notfound2'} and die; exists $cells->{'notfound3'} and die; } my $str_ref = str_hash(); my $st2_ref = str_hash(); my $big_ref = big_hash(); my $pak_ref = pak_hash(); my $mat_ref = mat_hash(); timethese 200000, { Str => sub { str_look($str_ref) }, St2 => sub { st2_look($st2_ref) }, Big => sub { big_look($big_ref) }, Pak => sub { pak_look($pak_ref) }, Mat => sub { mat_look($mat_ref) }, };

Benchmark from Perl 5.18.2 (default Perl) on Mac OS X 10.11.6.

Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 7 wallclock secs ( 6.73 usr + 0.00 sys = 6.73 CPU) @ 29717.68/s +(n=200000) Mat: 6 wallclock secs ( 5.38 usr + 0.00 sys = 5.38 CPU) @ 37174.72/s +(n=200000) Pak: 6 wallclock secs ( 6.43 usr + 0.00 sys = 6.43 CPU) @ 31104.20/s +(n=200000) St2: 5 wallclock secs ( 4.66 usr + 0.00 sys = 4.66 CPU) @ 42918.45/s +(n=200000) Str: 6 wallclock secs ( 5.72 usr + 0.00 sys = 5.72 CPU) @ 34965.03/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 6 wallclock secs ( 6.76 usr + 0.00 sys = 6.76 CPU) @ 29585.80/s +(n=200000) Mat: 4 wallclock secs ( 5.30 usr + 0.00 sys = 5.30 CPU) @ 37735.85/s +(n=200000) Pak: 6 wallclock secs ( 6.43 usr + 0.00 sys = 6.43 CPU) @ 31104.20/s +(n=200000) St2: 5 wallclock secs ( 4.65 usr + 0.00 sys = 4.65 CPU) @ 43010.75/s +(n=200000) Str: 6 wallclock secs ( 5.74 usr + 0.00 sys = 5.74 CPU) @ 34843.21/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 7 wallclock secs ( 6.79 usr + 0.00 sys = 6.79 CPU) @ 29455.08/s +(n=200000) Mat: 5 wallclock secs ( 5.32 usr + 0.00 sys = 5.32 CPU) @ 37593.98/s +(n=200000) Pak: 6 wallclock secs ( 6.43 usr + 0.00 sys = 6.43 CPU) @ 31104.20/s +(n=200000) St2: 3 wallclock secs ( 4.71 usr + 0.00 sys = 4.71 CPU) @ 42462.85/s +(n=200000) Str: 6 wallclock secs ( 5.74 usr + 0.00 sys = 5.74 CPU) @ 34843.21/s +(n=200000)

Benchmark from Perl 5.22.3 on Mac OS X.

config_args='-Dprefix=/opt/perl-5.22.3 -sder -Dusethreads -Accflags=-m +sse4.2'
Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 7 wallclock secs ( 6.07 usr + 0.00 sys = 6.07 CPU) @ 32948.93/s +(n=200000) Mat: 4 wallclock secs ( 4.64 usr + 0.00 sys = 4.64 CPU) @ 43103.45/s +(n=200000) Pak: 6 wallclock secs ( 5.26 usr + 0.00 sys = 5.26 CPU) @ 38022.81/s +(n=200000) St2: 4 wallclock secs ( 4.60 usr + 0.00 sys = 4.60 CPU) @ 43478.26/s +(n=200000) Str: 5 wallclock secs ( 4.77 usr + 0.00 sys = 4.77 CPU) @ 41928.72/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 6 wallclock secs ( 6.01 usr + 0.00 sys = 6.01 CPU) @ 33277.87/s +(n=200000) Mat: 5 wallclock secs ( 4.77 usr + 0.00 sys = 4.77 CPU) @ 41928.72/s +(n=200000) Pak: 5 wallclock secs ( 5.32 usr + 0.00 sys = 5.32 CPU) @ 37593.98/s +(n=200000) St2: 5 wallclock secs ( 4.67 usr + 0.00 sys = 4.67 CPU) @ 42826.55/s +(n=200000) Str: 5 wallclock secs ( 4.87 usr + 0.00 sys = 4.87 CPU) @ 41067.76/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 6 wallclock secs ( 5.96 usr + 0.00 sys = 5.96 CPU) @ 33557.05/s +(n=200000) Mat: 5 wallclock secs ( 4.65 usr + 0.00 sys = 4.65 CPU) @ 43010.75/s +(n=200000) Pak: 5 wallclock secs ( 5.28 usr + 0.00 sys = 5.28 CPU) @ 37878.79/s +(n=200000) St2: 4 wallclock secs ( 4.67 usr + 0.00 sys = 4.67 CPU) @ 42826.55/s +(n=200000) Str: 5 wallclock secs ( 4.88 usr + 0.00 sys = 4.88 CPU) @ 40983.61/s +(n=200000)

Benchmark from Perl 5.24.1 on Mac OS X.

config_args='-Dprefix=/opt/perl-5.24.1 -sder -Dusethreads -Accflags=-m +sse4.2'
Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 6 wallclock secs ( 5.93 usr + 0.00 sys = 5.93 CPU) @ 33726.81/s +(n=200000) Mat: 5 wallclock secs ( 4.66 usr + 0.00 sys = 4.66 CPU) @ 42918.45/s +(n=200000) Pak: 5 wallclock secs ( 5.24 usr + 0.00 sys = 5.24 CPU) @ 38167.94/s +(n=200000) St2: 5 wallclock secs ( 4.53 usr + 0.00 sys = 4.53 CPU) @ 44150.11/s +(n=200000) Str: 4 wallclock secs ( 4.64 usr + 0.00 sys = 4.64 CPU) @ 43103.45/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 6 wallclock secs ( 5.94 usr + 0.00 sys = 5.94 CPU) @ 33670.03/s +(n=200000) Mat: 5 wallclock secs ( 4.76 usr + 0.00 sys = 4.76 CPU) @ 42016.81/s +(n=200000) Pak: 5 wallclock secs ( 5.26 usr + 0.00 sys = 5.26 CPU) @ 38022.81/s +(n=200000) St2: 5 wallclock secs ( 4.49 usr + 0.00 sys = 4.49 CPU) @ 44543.43/s +(n=200000) Str: 5 wallclock secs ( 4.67 usr + 0.00 sys = 4.67 CPU) @ 42826.55/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 6 wallclock secs ( 5.96 usr + 0.00 sys = 5.96 CPU) @ 33557.05/s +(n=200000) Mat: 5 wallclock secs ( 4.66 usr + 0.00 sys = 4.66 CPU) @ 42918.45/s +(n=200000) Pak: 5 wallclock secs ( 5.38 usr + 0.00 sys = 5.38 CPU) @ 37174.72/s +(n=200000) St2: 5 wallclock secs ( 4.50 usr + 0.00 sys = 4.50 CPU) @ 44444.44/s +(n=200000) Str: 4 wallclock secs ( 4.68 usr + 0.00 sys = 4.68 CPU) @ 42735.04/s +(n=200000)

Regards, Mario

Replies are listed 'Best First'.
Re^2: Fastest way to lookup a point in a set
by marioroy (Prior) on Aug 05, 2017 at 08:59 UTC

    Update: Tried Perl's state feature at the end of the post.

    I ran under CentOS Linux release 7.3.1611, a Parallels Desktop VM, using the default Perl v5.16.3.

    I also tried pre-computing the points as strings for comparison. That reaches near 100k per second or double the performance of st2_look.

    { my @points_str; sub st3_look { my $cells = shift; unless (@points_str) { for my $p (@points) { push @points_str, join(':', @$p); } } for my $p (@points_str) { exists $cells->{$p} or die; } exists $cells->{'notfound'} and die; exists $cells->{'notfound2'} and die; exists $cells->{'notfound3'} and die; } } my $str_ref = str_hash(); my $st2_ref = str_hash(); my $st3_ref = str_hash(); my $big_ref = big_hash(); my $pak_ref = pak_hash(); my $mat_ref = mat_hash(); timethese 200000, { Str => sub { str_look($str_ref) }, St2 => sub { st2_look($st2_ref) }, St3 => sub { st3_look($st3_ref) }, Big => sub { big_look($big_ref) }, Pak => sub { pak_look($pak_ref) }, Mat => sub { mat_look($mat_ref) }, };

    Results from the same VM, CentOS 7.3, Perl v5.16.3.

    Another way is via Perl's state feature. However, it fails in list context.

    sub st3_look { my $cells = shift; state @points_str = map { join ':', @{$_} } @points; ... } Initialization of state variables in list context currently forbidden +at test.pl line 266, near "@points;" Execution of test.pl aborted due to compilation errors.

    But, works fine for an anonymous array.

    use feature qw(state); sub st3_look { my $cells = shift; state $points_str = [ map { join ':', @{$_} } @points ]; for my $p (@{ $points_str }) { exists $cells->{$p} or die; } exists $cells->{'notfound'} and die; exists $cells->{'notfound2'} and die; exists $cells->{'notfound3'} and die; }

    Regards, Mario