ちょっと気になったので、他の言語でもbenchmarkを取ってみたのですが、ひょっとしてpythonのposix moduleのbugをみっけたかも。

404 Blog Not Found:perl - PerlIO ":mmap" and other layers
というわけで、benchmarkをとってみました。
perl - test.txtの作成もこれにやらせる
#!/usr/bin/perl
use strict;
use warnings;
my $test_file = "test.txt";
my $test_line =
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_\n";

sub make_test_file {
    my $kb    = shift;              # size in KB
    my $block = $test_line x 16;    # 1024 bytes;
    my $fh;
    open $fh, ">:raw", $test_file or die "$test_file:$!";
    print {$fh} $block for ( 1 .. $kb );
    close $fh;
}

sub benchmark {
    my ($file, $line, $count) = @_;
    my (@start) = times();
    for ( 1 .. $count ) {
        open my $fh, "<", $file or die "$file:$!";
        while (<$fh>) {
            die if $_ ne $line;
        }
        close $fh;
    }
    my (@end) = times();
    $end[$_] -= $start[$_] for ( 0 .. 3 );
    printf "User: %f s; System: %f s\n", @end;
}

make_test_file(4096);
benchmark($test_file, $test_line, 100);

__END__
C - straight forwardな方法
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/times.h>
#define TEST_FILE "test.txt"
#define TEST_LINE \
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_\n"

int main(int argc, char **argv){
    int i;
    struct tms tm;
    double utime, stime;
    char buf[256];
    FILE *fp;

    times(&tm);
    utime = tm.tms_utime;
    stime = tm.tms_stime;

    for (i = 0; i < 100; i++){
        fp = fopen(TEST_FILE, "r");
        if (fp == NULL) exit(-1);
        while(fgets(buf, 256, fp)){
            if (strncmp(buf, TEST_LINE, 256) != 0) exit(-1);
        }
        fclose(fp);
    }

    times(&tm);
    utime = tm.tms_utime - utime;
    stime = tm.tms_stime - stime;
    printf("User: %f s; System: %f s\n", utime/100, stime/100);
}
ruby - open(file).readlines.each{} とかしない
#!/usr/bin/ruby
test_file = "test.txt"
test_line = \
    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_\n"

def benchmark(file, line, n)
    start = Process.times()
    n.times{
        fp = open(file) or exit
        while l = fp.gets
            l != line and exit();
        end
        fp.close
    }
    finish = Process.times()
    utime = finish.utime - start.utime;
    stime = finish.stime - start.stime;
    puts "User: #{utime} s; System: #{stime} s"
end

benchmark(test_file, test_line, 100)
python - posix.times()ってひょっとしてbraindead?
#!/usr/bin/python
import posix

test_file = "test.txt"
test_line \
 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_\n"

def benchmark(file, line, count):
    start = posix.times()    
    for n in range(count):
        fp = open(file)
        while 1:
            l = fp.readline()
            if not l:
               break
            if l != line:
               exit(-1)
        fp.close()
    finish = posix.times()
    utime  = finish[0] - start[0]
    stime  = finish[1] - start[1]
    print "User: %f s; System: %f s" % (utime, stime)

benchmark(test_file, test_line, 100)
実行結果:
% /usr/bin/time ./perl.pl 
User: 5.870000 s; System: 0.610000 s
        6.75 real         5.88 user         0.67 sys
% /usr/bin/time ./a.out 
User: 3.160000 s; System: 0.600000 s
        3.87 real         3.16 user         0.60 sys
% /usr/bin/time ./ruby.rb
User: 10.94 s; System: 0.71 s 
       11.69 real        10.94 user         0.71 sys
% /usr/bin/time ./python.py
User: 21.450000 s; System: 1.150000 s
       13.69 real        12.93 user         0.71 sys

な、なんじゃこりゃー!pythonのまわりには重力場でもあるんかいな!

いづれにせよ、ファイル読み込みに関してはperlはなかなか優秀そうだということがわかったというお話。

Dan the Python Novice