[ Perl tips index ]
[ Subscribe to Perl tips ]
#!/usr/bin/perl -w use strict; use Benchmark qw(:all); use List::Util qw(first); # A big, sorted list, with 10702 elements. my @list = (1..10000, 'a'..'zz'); my @selection; # push a bunch of random things that do exist onto the list foreach ( 1 .. 100 ) { push @selection, $list[rand @list]; } # and a bunch of things which don't (total length 152) foreach ( 'aaa' ... 'abz') { push @selection, $_; } my %exists; @exists{@list} = (); print "Comparing different sorting options small list over large list\n"; cmpthese(0, { # Looking for @selection in @list with grep 'grep' => sub { my $found; foreach my $find (@selection) { $found = grep { $_ eq $find } @list; } }, # Looking for @selection in @list with first 'first' => sub { my $found; foreach my $find (@selection) { $found = first { $_ eq $find } @list; } }, # Looking for @selection in @list linearly (with foreach) 'foreach' => sub { my $found; foreach my $find (@selection) { foreach (@list) { if($_ eq $find) { $found = 1; last; } } } }, # Looking for @selection in a hash of @list 'lb-hash' => sub { my $found; my %hash; # build hash @hash{@list} = (); foreach my $find (@selection) { $found = exists $hash{$find}; } }, # Looking for @list against a list of @selection (short circuiting) 'w-hash' => sub { my %hash; @hash{@selection} = (); foreach my $find (@list) { delete $hash{$find}; last unless keys %hash; } }, # Looking for @selection in pre-built @exists 'pre-built hash' => sub { my $found; foreach my $find (@selection) { $found = exists $exists{$find}; } }, });
[ Perl tips index ]
[ Subscribe to Perl tips ]
| Location | Course | Course Date | Duration | Early Bird Date |
|---|---|---|---|---|
| Melbourne | Programming Perl | Mon 16 Aug 2010 | 5 days | — |
| Sydney | Programming Perl | Mon 6 Sep 2010 | 5 days | Tue 3 Aug 2010 |
| Canberra | Programming Perl | Mon 20 Sep 2010 | 5 days | Tue 24 Aug 2010 |
| Melbourne | Enterprise Perl | Mon 11 Oct 2010 | 5 days | Fri 24 Sep 2010 |
| Sydney | Enterprise Perl | Mon 25 Oct 2010 | 5 days | Tue 21 Sep 2010 |
| Canberra | Enterprise Perl | Mon 8 Nov 2010 | 5 days | Tue 5 Oct 2010 |
For future dates, please see our training calendar.
This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.
If you have any questions please don't hesitate to contact us:
| Email: | contact@perltraining.com.au |
| Phone: | 03 9354 6001 (Australia) |
| International: | +61 3 9354 6001 |
Copyright 2001-2010 Perl Training Australia. Contact us at contact@perltraining.com.au