PHPとRubyとPythonでのCSVデータ操作のベンチマーク

ちょっと気になってCSV処理のベンチを取ってみた。
ルール的には、なるべく標準に付いてるCSV処理を行うものを利用して、TSVデータに変換すること。

Rubyに関しては、標準のCSVライブラリで処理を行ったら、3時間経っても終わらなかったので、File.openを利用するベーシックなやり方でやってみた。

データに利用したのは、郵便番号のデータ 全国一括版
約12万件あるそうだ。

ベンチマークには以下を利用

ベンチ結果

PHP 5.2.8
---------------------------------------------
marker  time index            ex time        
---------------------------------------------
total   -                     7.1641280651093
---------------------------------------------
Ruby 1.8.7p72
    user     system      total        real
5.910000   0.160000   6.070000 (  6.088126)
Ruby 1.9.1
    user     system      total        real
7.420000   0.160000   7.580000 (  7.579139)
Python-2.6.1
Test                             minimum  average  operation  overhead
-------------------------------------------------------------------------------
Totals:                           9714ms   9900ms
Python-3.0
Test                             minimum  average  operation  overhead
-------------------------------------------------------------------------------
Totals:                          10593ms  10706ms

CSV使わないとRubyが早いですねぇ
Pythonが早いと聞いていたけど、今回はマシンのせいなのかな?

ベンチに利用したスクリプト

PHP
<?php
// vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
require 'Benchmark/Timer.php';
$timer = new Benchmark_Timer();
$timer->start();

$fp = new SplFileObject('ken_all.csv', 'r');
$write = new SplFileObject('result.tsv', 'w');
while ($data = $fp->fgetcsv()) {
    $write->fwrite(implode("\t", $data) . "\n");
}
$timer->display();
Ruby
# vim:set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
require 'benchmark'

puts Benchmark::CAPTION
puts Benchmark.measure {
  File.open('result.tsv', 'w') do |f|
    File.open('ken_all.csv', 'r') do |r|
      r.each do |data|
        f.write(data.gsub(/"(.*?)"/, '\1').split(/,/).join("\t"))
      end
    end
  end
}
Python
# vim:set expandtab tabstop=4 softtabstop=4:
import csv

f = open('result.tsv', 'w')
for line in csv.reader(file("ken_all.csv")):
    f.write("\t".join(line))</pre></blockquote>