1. 程式人生 > >python和php的文字處理速度對比

python和php的文字處理速度對比

執行環境:

PHP 5.4.17

Python 2.6.6

linux 64位

一、逐行讀取資料檔案,用tab鍵做分隔處理

split.py 

#!/usr/bin/python
#coding=utf-8

import sys

if len(sys.argv) != 2:
        print sys.argv[0]+" [in_file]\n逐行讀取資料,做'\\t'分隔"
        exit()


in_file = sys.argv[1]

n = 0
for row in open(in_file):
        temp = row.split('\t')
        n += 1

print n
split.php
<?php

if (count($argv) != 2) {
        echo $argv[0]," [in_file]\n逐行讀取檔案,然後‘\\t’分隔";
        exit;
}

$in_file = $argv[1];

$fp = fopen($in_file, 'r');

$n = 0;
while(($row=fgets($fp, 4096))!==false){
        $arr = explode("\t", $row);
        ++$n;
}

echo $n;

執行速度對比

[root@localhost php_py]# time python split.py data.txt 
730408

real	0m1.705s
user	0m1.605s
sys	0m0.093s
[root@localhost php_py]# time php split.php data.txt 
730408
real	0m3.261s
user	0m3.144s
sys	0m0.111s