またPerlの話

ベンチマークというものがあるらしい。ふむふむ。

http://www.pure.co.jp/~learner/program/Perl_tips.html

で、やってみた。

#!/usr/bin/perl -w
# test011.pl
# created:2005-02-27 02:04:38+09:00
#
# benchmark

use strict;
use Benchmark;

my $count = 2000;
my %codes = ();

$codes{'code1'} = q{
	my @input_numbers;
	my $count;

	for (1..40000) {
		push @input_numbers, (int(rand(100))+ 1);
	}

	$count = my @list = grep {my ($number) = $_;
		if ($number % 7) {
			0;
		} else {
			1;
		}} @input_numbers;

};

$codes{'code2'} = q{
	my $count = my @list = grep{if($_ % 7){0;}else{1;}} map{int(rand(100)) + 1}(1..40000);
};

timethese($count,\%codes);

# test011.pl ends here.

結果は、

Benchmark: timing 2000 iterations of code1, code2...
     code1: 207 wallclock secs (203.62 usr +  0.15 sys = 203.77 CPU) @  9.81/s (n=2000)
     code2: 152 wallclock secs (141.89 usr +  0.07 sys = 141.96 CPU) @ 14.09/s (n=2000)

と表示された。多分pushするところで処理に時間がかかっているのだろうなぁ。(自信はないけれど)

話は変わるが、だんだんとPerlの書き方がLispっぽくなってきているような気がしてきた。