camel

Perl 5.8�ʹߤǤϡ����Τ褦�ʾ���in-memory file���Ȥ��ޤ���

��³�ۤ�Ϥ� Perl �ϥ�������ʸ��졣�ǡ�������������¤ :: Drk7jp
�ģ¾�����쥳���ɤ򤤤ä��� perl ¦������˳�Ǽ���ơ����η�̤��֤����äƥ����ɤʤΤǤ����������ʤ���쥳���ɿ���¿���ʤ�Х���򿩤��Τ����������ʤΤǤ����������ε��������Ƥ�����˺�Ѥ��Ƥޤ����������ˤ���Ȥ��꤬����ΤǤ���

�Ȥ����ϡ���ñ�Ǥ���

my @array = (0x21..0x7e);
my $memfile;

open my $wfh, '>', \$memfile or die $!;
print $wfh chr($_), "\n" for (@array);
close $wfh;

open my $rfh, '<', \$memfile or die $!;
print while(<$rfh>);
close $wfh;

�⤷array�����Ƥ�sequential�ˤ��������������ʤ��ΤǤ���С�������ˡ�Ƿ�Ū�˥��꡼�����̤򸺤餻�ޤ���

# ���code��append
use Devel::Size::Report qw/report_size/;
print report_size \@array;
print report_size \$memfile;
Size report v0.10 for 'ARRAY(0x181832c)':
  Array ref 1948 bytes (overhead: 444 bytes, 22.79%)
    Scalar 16 bytes
    Scalar 16 bytes
����....
Total: 1948 bytes in 95 elements
Size report v0.10 for 'SCALAR(0x1801b1c)':
  Scalar Ref 228 bytes (overhead: 16 bytes, 7.02%)
    Scalar 212 bytes
Total: 228 bytes in 2 elements

������󡢤��Τ�����Ǥ�speed�Ȥ�trade-off�Ϥ���ޤ���

#!/usr/local/bin/perl
use strict;
use warnings;
use Benchmark qw/timethese cmpthese/;

cmpthese(timethese(
        0, {
            array => sub {
                my @array = ();
                push @array, $_ for ( 0 .. 0xff );
                for ( 0 .. 0xff ) {
                    $array[$_] != $_ and die;
                }
            },
            memfile => sub {
                my $memfile = '';
                open my $wfh, '>', \$memfile or die;
                print $wfh $_, "\n" for ( 0 .. 0xff );
                close $wfh;
                open my $rfh, '<', \$memfile or die;
                for ( 0 .. 0xff ) {
                    my $line = <$rfh>;
                    $_ == $line or die;
                }
                close $rfh;
              }
          }
));
__END__
MacBook Pro 2.0GHz �Ǥη��:
Benchmark: running array, memfile for at least 3 CPU seconds...
     array:  4 wallclock secs ( 3.13 usr +  0.01 sys =  3.14 CPU) @ 7003.82/s (n=21992)
   memfile:  4 wallclock secs ( 3.18 usr +  0.01 sys =  3.19 CPU) @ 2334.80/s (n=7448)
          Rate memfile   array
memfile 2335/s      --    -67%
array   7004/s    200%      --

®�٤�1/3�ˤʤ�ޤ��������꡼��Ψ�ϣ��ܡ������������ʤ��Ȼפ��ޤ����ޤ��Ƥ�swap�ʤɤΤ��Ȥ�ͤ���С�

�ʾ�ϰ�԰�쥳���ɤξ��Ǥ���������Ĺ�ξ��Ϥ����efficient�˽���ޤ����㤨�С��쥳���ɤ����٤�Unsigned Long�Ǥ��뤳�Ȥ��狼�äƤ���С�

open my $wfh, '>', \$memfile or die $!;
print $wfh pack("N", $_) for (@array);
close $wfh;

open my $rfh, '<', \$memfile or die $!;
print unpack("N", $_), "\n" while(read($rfh, $_, 4));
close $wfh;

�Τ褦�˽���ޤ���

�����Ȥä�Wrapper Class���ä���Tie::Array::Whatever�⥸�塼�����Τ�Trivial�Ǥ��礦��

�Ǹ�ˡ�������ǽ�ˤ��Ƥ��줿����˴��Nick Ing-Simmons�˴��դ��Ĥ���entry��submit���ޤ���

Dan the Just Another PerlIO Hacker