camel

���ΤϤޤ����ä��衢�Ҥ�������

�٤�each������˻Ȥ�splice�Υ��åȡ��ǥ��å� | ï¿½Ñ¥ë¥«ï¿½ï¡ª
�ǡ�������٤�each��Ȥ�����˹�®��splice�Ϥɤ��Ǥ��礦���ä��á�

���ˤ��äơ�������������ꤲ��ʤ��!

# ��
my %mapping = my @mapping = map {$_ => $_} (1..100); # �����ǽ�������Ƥ��뤱�ɡ�

cmpthese -1, {
    splice => sub {
        while (my ($key, $value) = splice @mapping, 0, 2) {}
    },
    # ��
};
use strict;
use warnings;
use Benchmark qw(:all);

print "splice(), each(), keys(), values() Benchmark!!\n";

cmpthese -1, {
    splice => sub {
        my @mapping = %hash; # each()�������ˤ��뤿��ˤϡ��������ʤ���
        while (my ($key, $value) = splice @mapping, 0, 2) {}
    },
    # ��
};

���ʤ�ȩ��Υ������������Ƥ��������顢�����������Τ���¿�ˤ��뤳�Ȥ���ʤ�������ɡ�

���ȡ����Υ����ɡ�

each()���٤������̯������ⵯ���䤹�� - Islands in the byte stream
cmpthese -1, {
    # ��
    keys => sub {
        foreach my $key(keys %hash) { } # ����� each ����������ʤ����?
    },
    # ��
};
cmpthese -1, {
    # ��
    keys => sub {
        # �����ˤ���ʤ顢������
        foreach my $key ( keys %hash ) { my $value = $hash{$key}; }
    },
    # ��
};

�ǡ��嵭��ȿ�Ǥ�������̤��������顣

          Rate  splice each_kv  each_k     map    keys  values
splice   135/s      --    -41%    -63%    -65%    -66%    -91%
each_kv  226/s     68%      --    -37%    -41%    -42%    -85%
each_k   359/s    167%     59%      --     -7%     -9%    -76%
map      384/s    186%     70%      7%      --     -2%    -75%
keys     393/s    192%     74%      9%      2%      --    -74%
values  1520/s   1030%    571%    323%    295%    287%      --

splice��keys�ɤ�����each�ˤ�ڤФʤ��

�Ȥ����櫓�ǡ�hash�����Ψ�ɤ����ѤǤ��롢����ѥ��Ȥǰ����ʹ�ʸ�Ϥ�����

for my $key (keys %hash) {
    my $value = $hash{$key};
    # and use $key and $value here
}

�ºݡ��ϥå���򥤥ƥ졼�Ȥ���ݤˤ�sort���Ȥ߹�碌�뤳�Ȥ�¿���Τǡ��Ȥ��䤹���⤢�롣

for my $key (sort keys %hash) {
    my $value = $hash{$key};
    # and use $key and $value here
}

�ȤϤ�����%hash��$key�����ٽ񤫤ʤ��Ȥ����ʤ��Ȥ����������Ƥʤ��Ȥϻ��פ���hash.each{ |k,v| #�� } ����ľ�˽񤱤�Rubyists�Ρ֤櫓���狼��ʤ���פȤ�������ʹ�����Ƥ���������

Dan the Perl Monger

#!perl
use strict;
use warnings;
use Benchmark qw(:all);

my %hash = map { $_ => $_ } ( 1 .. 10000 );

cmpthese timethese - 1, {
    each_k => sub {
        while ( my $key = each %hash ) { }
    },
    each_kv => sub {
        while ( my ( $key, $value ) = each %hash ) { }
    },
    keys => sub {
        foreach my $key ( keys %hash ) { my $value = $_; }
    },
    values => sub {
        foreach my $value ( values %hash ) { }
    },
    map => sub {
        map { my $key = $hash{$_} } keys %hash;
    },
    splice => sub {
        my @keys = %hash;
        while ( my ( $key, $value ) = splice @keys, 0, 2 ) { }
    },
};