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.
Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 10 wallclock secs ( 6.16 usr + 0.0 sys = 6.16 CPU) @ 32467.53/s +(n=200000) Mat: 4 wallclock secs ( 4.72 usr + 0.0 sys = 4.72 CPU) @ 42372.88/s +(n=200000) Pak: 5 wallclock secs ( 5.39 usr + 0.0 sys = 5.39 CPU) @ 37105.75/s +(n=200000) St2: 4 wallclock secs ( 4.11 usr + 0.0 sys = 4.11 CPU) @ 48661.80/s +(n=200000) Str: 5 wallclock secs ( 4.88 usr + 0.0 sys = 4.88 CPU) @ 40983.61/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 5 wallclock secs ( 6.13 usr + 0.0 sys = 6.13 CPU) @ 32626.43/s +(n=200000) Mat: 3 wallclock secs ( 4.68 usr + 0.0 sys = 4.68 CPU) @ 42735.04/s +(n=200000) Pak: 5 wallclock secs ( 5.38 usr + 0.0 sys = 5.38 CPU) @ 37174.72/s +(n=200000) St2: 4 wallclock secs ( 4.11 usr + 0.0 sys = 4.11 CPU) @ 48661.80/s +(n=200000) Str: 5 wallclock secs ( 4.88 usr + 0.0 sys = 4.88 CPU) @ 40983.61/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, Str... Big: 6 wallclock secs ( 6.12 usr + 0.0 sys = 6.12 CPU) @ 32679.74/s +(n=200000) Mat: 4 wallclock secs ( 4.71 usr + 0.0 sys = 4.71 CPU) @ 42462.85/s +(n=200000) Pak: 10 wallclock secs ( 5.40 usr + 0.0 sys = 5.40 CPU) @ 37037.04/s +(n=200000) St2: 3 wallclock secs ( 4.09 usr + 0.0 sys = 4.09 CPU) @ 48899.76/s +(n=200000) Str: 5 wallclock secs ( 4.97 usr + 0.0 sys = 4.97 CPU) @ 40241.45/s +(n=200000)
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.
Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, St3, Str... Big: 6 wallclock secs ( 6.17 usr + 0.0 sys = 6.17 CPU) @ 32414.91/s +(n=200000) Mat: 4 wallclock secs ( 4.77 usr + 0.0 sys = 4.77 CPU) @ 41928.72/s +(n=200000) Pak: 6 wallclock secs ( 6.09 usr + 0.0 sys = 6.09 CPU) @ 32840.72/s +(n=200000) St2: 4 wallclock secs ( 4.29 usr + 0.0 sys = 4.29 CPU) @ 46620.05/s +(n=200000) St3: 2 wallclock secs ( 2.09 usr + 0.0 sys = 2.09 CPU) @ 95693.78/s +(n=200000) Str: 5 wallclock secs ( 4.92 usr + 0.0 sys = 4.92 CPU) @ 40650.41/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, St3, Str... Big: 6 wallclock secs ( 6.19 usr + 0.0 sys = 6.19 CPU) @ 32310.18/s +(n=200000) Mat: 8 wallclock secs ( 4.78 usr + 0.0 sys = 4.78 CPU) @ 41841.00/s +(n=200000) Pak: 6 wallclock secs ( 5.42 usr + 0.0 sys = 5.42 CPU) @ 36900.37/s +(n=200000) St2: 5 wallclock secs ( 4.15 usr + 0.0 sys = 4.15 CPU) @ 48192.77/s +(n=200000) St3: 2 wallclock secs ( 2.18 usr + 0.0 sys = 2.18 CPU) @ 91743.12/s +(n=200000) Str: 5 wallclock secs ( 5.00 usr + 0.0 sys = 5.00 CPU) @ 40000.00/s +(n=200000) Benchmark: timing 200000 iterations of Big, Mat, Pak, St2, St3, Str... Big: 6 wallclock secs ( 6.18 usr + 0.0 sys = 6.18 CPU) @ 32362.46/s +(n=200000) Mat: 5 wallclock secs ( 4.66 usr + 0.0 sys = 4.66 CPU) @ 42918.45/s +(n=200000) Pak: 5 wallclock secs ( 5.37 usr + 0.0 sys = 5.37 CPU) @ 37243.95/s +(n=200000) St2: 3 wallclock secs ( 4.09 usr + 0.0 sys = 4.09 CPU) @ 48899.76/s +(n=200000) St3: 1 wallclock secs ( 2.11 usr + 0.0 sys = 2.11 CPU) @ 94786.73/s +(n=200000) Str: 4 wallclock secs ( 4.90 usr + 0.0 sys = 4.90 CPU) @ 40816.33/s +(n=200000)
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
In reply to Re^2: Fastest way to lookup a point in a set
by marioroy
in thread Fastest way to lookup a point in a set
by eyepopslikeamosquito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |