> CentOS > CentOS故障 >

Extracting values from a list of (key, value) pairs

If you need ordered key, value pairs, you can either use something like Tie::IxHash or a simple array of key, value pairs. I found myself in the situation where I needed to extract just the keys from such an array.

There are a number of ways to do it, but which is the fastest? I tried a few pure Perl approaches, as well as List::Util::pairkeys (which as of this writing isn't yet in a stable release of Perl, assuming that List::Util remains in the core). The pure Perl approaches either use various means of flipping a binary toggle, or splice() ing through a sacrificial copy of the array.

Here we go:

#!/usr/bin/env perl

use 5.10.1;

use List::Util qw[ pairkeys ];

use Benchmark qw[ timethese cmpthese ];

my $N = 10000;

my @N = 0 .. $N;

cmpthese(
    timethese(
        10000,
        {

            'splice' => sub {
                my @v;
                my ( $k, $v );
                my @N = @N;
                push @v, $k while ( ( $k, $v ) = splice( @N, 0, 2 ) );

            },

            '% map' => sub {
                use integer;
                my $flip = 0;
                my @v    = map { ++$flip % 2 ? $_ : () } @N;
            },

            '% grep' => sub {
                use integer;
                my $flip = 0;
                my @v    = grep { ++$flip % 2 } @N;
            },

            '1- map' => sub {
                use integer;
                my $flip = 0;
                my @v
                  = map { ( $flip = 1 - $flip ) ? $_ : () } @N;
            },

            '1- grep' => sub {
                use integer;
                my $flip = 0;
                my @v    = grep { $flip = 1 - $flip } @N;
            },

            'flipflop' => sub {
                use integer;
                my $flip = 0;
                my @v    = grep { $flip = !( $flip .. $flip ) } @N;
            },

            'pairkeys' => sub {
                my @v = pairkeys @N;
            },

        } ) );

And here are the results:

           Rate flipflop   splice    % map   1- map   % grep  1- grep pairkeys
flipflop  480/s       --      -7%     -17%     -29%     -43%     -57%     -75%
splice    518/s       8%       --     -11%     -23%     -38%     -53%     -73%
% map     580/s      21%      12%       --     -14%     -31%     -48%     -70%
1- map    675/s      41%      30%      16%       --     -19%     -39%     -65%
% grep    838/s      75%      62%      44%      24%       --     -25%     -57%
1- grep  1112/s     132%     115%      92%      65%      33%       --     -43%
pairkeys 1942/s     305%     275%     235%     188%     132%      75%       --
  • If you've got List::Util::pairkeys , use it.
  • grep is faster than map ; not too surprising
  • Remainders (%) are slower then subtraction; not too surprising.
  • In this application use integer provided a significant boost in speed.
  • The flipflop operator is surprisingly slow
  • splice() and flipflop are essentially tied. splice() is slowed down significantly by needing to make a copy of the array. (If I added the copy cost to the others it moved up two slots in the rankings). It would be even slower if the array (or its elements) were larger. When Copy on Write (COW) makes it into Perl, that difference should diminish.
(责任编辑:IT)