- 论坛徽章:
- 0
|
1.pl:
#!/usr/bin/perl -w
use strict;
use POSIX qw(strftime);
my $t1=strftime "%Y%m%d%H%H%S",localtime;
my $date=strftime "%Y%m%d",localtime;
my $log="/log${date}0000.dat";
chomp (my $ok=`/bin/grep 'Login OK' $log | wc -l`);
my $incorrect=`/bin/grep 'Login incorrect' $log | wc -l`;
open FILE,"> /log/radius_tongji.log" or die "$!";
print FILE $date . "\t" . $ok . "\t" . $incorrect;
close FILE;
my $t2=strftime "%Y%m%d%H%H%S",localtime;
print $t2-$t1 . "\n";
2.pl:
#!/usr/bin/perl -w
use strict;
use POSIX qw(strftime);
my ($ok,$incorrect);
my $t1=strftime "%Y%m%d%H%H%S",localtime;
my $date=strftime "%Y%m%d",localtime;
my $log="/log${date}0000.dat";
open FILE,"< $log" or die "$!";
while (<FILE>)
{
$ok++ if /Login OK/i;
$incorrect++ if /Login incorrect/i;
}
close FILE;
open FILE2,">> /log/radius_tongji.log" or die "$!";
print FILE2 $date . "\t" . $ok . "\t" . $incorrect . "\n";
close FILE2;
my $t2=strftime "%Y%m%d%H%H%S",localtime;
print $t2-$t1 . "\n";
运行1.pl花了13秒,运行2.pl花了22秒。
但感觉1.pl嵌套了shell效率不是很高,而2.pl是纯的perl语句,但效率不如1.pl.
请教各位,选择哪个呢?还有其他更好的办法吗?
[ 本帖最后由 gaochong 于 2009-6-16 16:49 编辑 ] |
|