On Jan 22, 7:18 pm, [EMAIL PROTECTED] (Paul Johnson) wrote:
> On Tue, Jan 22, 2008 at 05:52:35PM -0500, Chas. Owens wrote:
> > On Jan 22, 2008 2:58 PM, lerameur <[EMAIL PROTECTED]> wrote:
> > > Hello,
>
> > > I wrote a short perl script (65 lines), simply to count some log file
> > > time, and take the average. The script takes 4 minutes to run and go
> > > through about 8 millions lines.
> > > I would like to know if I can make it run faster. Why?, if I use the
> > > command 'wc -l filename' , I get the number of lines in about a
> > > minute, that is three less then the small script. I am right by
> > > thinking the script can be reprogrammed so it can be process the file
> > > faster ???
> > snip
>
> > Since we can't see your code, we can't tell if it can be done faster.
> > Please note that wc -l is doing something that in Perl could be
> > accomplished in 1 line:
>
> > perl -nle 'END { print $c } $c++' file
>
> > or the slightly more efficient
>
> > perl -ne 'BEGIN { $/ = \4196 } END { print "$c\n" } $c += tr/\n//' file
>
> > so 65 lines is a significantly more complex program and trying to
> > compare them is not a good idea. You should also make sure that you
> > are not seeing the effects of caching when comparing the two programs.
>
> Yes, give wc something to do and it will probably take a little longer.
>
> For example, on my machine here
>
> wc -w big_file
>
> takes about twice as long as
>
> perl -nle '$w++ while /\S+/g; END { print $w }' big_file
>
> which just goes to prove ... er ... hmmm ... well, not very much, really.
>
Well I was hoping to get something like, Yes perl is slower then
running perl script or anything
here is my program:
It takes the two times that are in the traffic log, takes the
difference and calculates the average of the difference for all the
lines.
#!/usr/bin/perl -w
use POSIX qw(ceil floor);
my $count=0;
my $old_time_difference=0;
sub printout{
my $time_second1=substr($date1,-4,2);
my $time_minute1=substr($date1,-6,2);
my $time_hour1=substr($date1,-8,2);
my $time_day1=substr($date1,-10,2);
my $time_month1=substr($date1,-12,2);
my $time_year1=substr($date1,-14,2);
my $time_second2=substr($date2,-4,2);
my $time_minute2=substr($date2,-6,2);
my $time_hour2=substr($date2,-8,2);
my $time_day2=substr($date2,-10,2);
my $time_month2=substr($date2,-12,2);
my $time_year2=substr($date2,-14,2);
#difference between the two time
my $total_seconds1=$time_second1+($time_minute1*60)+
($time_hour1*60*60)+($time_day1*24*60*60);
my $total_seconds2=$time_second2+($time_minute2*60)+
($time_hour2*60*60)+($time_day2*24*60*60);
my $time_difference= $total_seconds1-$total_seconds2;
$old_time_difference = ( $time_difference + $old_time_difference );
$count++;
my $average = $old_time_difference / $count;
}
################################################
############ Main Program ###########################
################################################
my $file_to_print = ( `ls -1c /home/etc/*log | tail -1 `);
#determine the latest and last document
print "file_to_print: $file_to_print";
open (FILE, "< $file_to_print") or die "Could not open file_to_print
$: $!";
$count =1;
while (my $line = <FILE> ) {
#print "$count\n";
if ( $line =~ m/protocol/ ) {
@items = split(",",$line);
$date1= $items[15];
$date2= $items[20];
$date3= $items[18];
if($date3 == 2008){
&printout;
}
else{$count++ ;
}
}#end if loop
} # end while loop
my $average_time = $old_time_difference / $count;
$average_time=int($average_time*1000);
$average_time=($average_time/1000);
print "Average time: $average_time Seconds\n";
close(FILE) or die "couldn't close file $!"
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/