Perl与shell的性能比较

Posted by 猪头小队长 | 程序设计 | Thursday 13 December 2007 22:26

分别用shell和perl逐行读一个450M的文本文件,并写入另一文件。

#!/bin/sh
while read line
do
    echo $line >> a;
done < "file4read.txt"

 

#!/usr/bin/perl
open ( readfile , "file4read.txt") or die ("Could not open the filen");
open ( writefile , ">b" );
while( $line = <readfile> )
{
    print writefile $line;
}
close( writefile );
close( readfile );

测试结果:

[root@test]# time ./read.sh 

real    13m18.797s
user    9m22.840s
sys     3m30.290s
[root@test]#
[root@test]# time ./read.pl

real    0m55.948s
user    0m10.720s
sys     0m2.980s
[root@test]#

 相当明显! 

1 Comment

  1. Comment by null — 2008/12/04 @ 13:09

    kilnt

    shell 的读文件循环方式不同,效率差别很大。

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.