perl几个读取文件命令

来源:互联网 发布:apache 报错日志 编辑:程序博客网 时间:2024/06/06 01:45

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0in;margin-bottom:.0001pt;mso-pagination:widow-orphan;font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;}@page Section1{size:8.5in 11.0in;margin:1.0in 1.25in 1.0in 1.25in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}div.Section1{page:Section1;}-->



####################
方案一 while 输出 最常用

open (FILES "index");
while (){
print;
}
close(FILES);
程序占用 CPU 时间:0.23 usr + 0.22 sys 合计运行时间 450 毫秒
#######################
方案二: 数组读取输出
open (FILES "index");
@reads=;
print @reads;


程序占用 CPU 时间:4.18 usr + 0.09 sys 合计运行时间 4270 毫秒 这个方法也常用,但效率最低的

换为@_;
open (FILES "index");
@_=;
print @_;
程序占用 CPU 时间:3.64 usr + 0.11 sys 合计运行时间 3750 毫秒 节省一点时间:)

################################
方案三:

open (FILES "index");
@_=;

#print @_;
close(FILES);
foreach (@_){
print;
}

程序占用 CPU 时间:3.79 usr + 0.15 sys 合计运行时间 3940 毫秒
很希奇,和上面相同,说明,只要打印数组,就和foreach处理方式相同
##############################
方案4 不打印
open (FILES "index");
@_=;
#print @_;
close(FILES);
程序占用 CPU 时间:3.45 usr + 0.00 sys 合计运行时间 3450 毫秒 还是相同
更奇怪,少一次循环,为什么时间还相同

#########################################
方案6,用变量包办数组直接读取输出。

open (FILES "index");
$/="";
$reads=;
print $reads;
close(FILES);
$/="/n";

程序占用 CPU 时间:0.04 usr + 0.00 sys 合计运行时间 40 毫秒 真快丫

##############################################
方案7 用分割变量来从新生成数组,包办数组@_= 直接读取方式,循环输出
open (FILES "index");
$/="";
$reads=

close(FILES);
$/="/n";

@_=split ( //n/$reads);
foreach (@_){
print $_;
}


程序占用 CPU 时间:0.34 usr + 0.11 sys 合计运行时间 450 毫秒

有趣,比普通@_= 直接读的方法要快10倍,但很是不解

假如数组不全部输出,单纯赋值占用时间很短
open (FILES "index");
$/="";
$reads=

close(FILES);
$/="/n";

@_=split ( //n/$reads);

程序占用 CPU 时间:0.05 usr + 0.00 sys 合计运行时间 50 毫秒

###############################################

散列
--------------------------------------------------------------------------------

open (FILES "index");
while (){$i++;
$hash{$i}=($_)
}
print %hash;

close(FILES);


程序占用 CPU 时间:0.66 usr + 0.41 sys 合计运行时间 1070 毫秒

数组的最快方式是450毫秒,散列全部输出时间大概比数组慢一倍

散列不输出
open (FILES "index");
while (){$i++;
$hash{$i}=($_)

}
#print %hash;

close(FILES);
程序占用 CPU 时间:0.09 usr + 0.04 sys 合计运行时间 130 毫秒 还是比数组慢

hash中取出一个元素

open (FILES "index");
while (){$i++;
$hash{$i}=($_)
}
close(FILES);
print $hash{8900};

程序占用 CPU 时间:0.13 usr + 0.00 sys 合计运行时间 130 毫秒 几乎不增加时间

数组也是,测不出时间
open (FILES "index");
$/="";
$reads=;

close(FILES);
$/="/n";

@_=split ( //n/$reads);

print $_[8900];

程序占用 CPU 时间:0.05 usr + 0.00 sys 合计运行时间 50 毫秒
##########################
以上测试结果有的差别很大,有的差别不大。

即使有些差别虽然是细微的,程序中如果把这些命令都采用最优化方式,相信会有效果的

我最近在把我的论坛调用比较频繁的核心代码进行逐行检查,反复测试优化中~~~~

谁把坡见的方法都测试以下,拿出来给兴趣者继承分享,我也继续测试中,今晚测试系统级IO命令,看有没有差别

仅供参考。

附,my测试方法
#########################
use CGI::Carp qw(fatalsToBrowser);
use Benchmark;
$TT0 = new Benchmark;
print "Content-type: text/html/n/n";
open (FILES "index");
$/="";
$reads=;

close(FILES);
$/="/n";

@_=split ( //n/$reads);
print @_;

###########################
print"";
$TT1 = new Benchmark;
$td = Benchmark::timediff($TT1 $TT0);
$td = Benchmark::timestr($td);
$td =~ /(/d+)/s*wallclock secs/(/s*?(/d*?/./d*?)/s*usr/s*/+/s*(/d*?/./d*?)/s*sys/i;
my $alltimas=($2+$3)*1000;
print "
程序占用CPU 时间:$2usr + $3 sys 合计运行时间$alltimas 毫秒";

exit;