
AWK vs PERL speed question
: I have an AWK script that I can run that basically searches through a
: file looking for match using this script:
: "BEGIN { FOUND=0; } /^[0]*1547/ { FOUND=1; print; next; } /^\/\// { if
: (FOUND) { print; next } else { next } } { if (FOUND) { exit } else {
: next } } " filename
: Using AWK this process takes about .85 seconds to execute.
: I have (based on very limited PERL knowledge) translated the above
: script into the following PERL script:
[script deleted]
: Does anyone have an alternative way to write this script so that it will
: run faster?
Depending on your CPU and I/O speed, part of the difference is
Perl's load time -- you can't get around that. But this should
cut your number of ops per line down, especially on that critical
first loop:
open(MSGFILE, "$errfile") ||
die "ERROR - Can't open error message file $errfile...exiting\n";
while (<MSGFILE>) {
(print, last) if /^[0]*1547/;
}
while (<MSGFILE>) {
(print, next) if m#//#;
last;
}
close MSGFILE;
Of course my awk (and system!) may not be as fast as yours, but on a
38,400 line file, with the match at line 38,000, this was awk:
bill:/tmp% date; awk -f test.awk < test.words ; date
Tue Feb 27 15:18:11 EST 1996
00001547
//
//
//
Tue Feb 27 15:19:06 EST 1996
bill:/tmp%
This was Perl, with my program above:
bill:/tmp% date ; perl test.pl < test.words ; date
Tue Feb 27 15:22:19 EST 1996
00001547
//
//
//
Tue Feb 27 15:22:23 EST 1996
bill:/tmp%
4 seconds for Perl vice 47 seconds for awk. I don't know if
it is the awk on my Linux, but....
--
Regards,
Mike Heins [mailed and posted] http://www.iac.net/~mikeh ___ ___
Internet Robotics |_ _|____ |_ _|
Few blame themselves until they 131 Willow Lane, Floor 2 | || _ \ | |
have exhausted all other Oxford, OH 45056 | || |_) || |
possibilities. |___| _ <|___|