Newsgroups: comp.lang.perl
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!van-bc!ubc-cs!alberta!herald.usask.ca!ccu.umanitoba.ca!rahardj
From: rahardj@ccu.umanitoba.ca (Budi Rahardjo)
Subject: pattern matching performance
Message-ID: <1991May6.161906.21161@ccu.umanitoba.ca>
Followup-To: comp.lang.perl
Keywords: grep, pattern matching
Sender: rahardj@ccu.umanitoba.ca (Budi Rahardjo)
Organization: University of Manitoba, Winnipeg, Canada
Distribution: world
Date: Mon, 6 May 91 16:19:06 GMT
Lines: 37

I was wondering if anybody could show me the fastest way to match
a pattern in perl. I have a big flatfile (around 17000 lines).
Using UNIX grep takes around 1 or 2 second, but perl's pattern
matching takes 12 secs. 
Need advice ...

-- budi

Here is a simplified benchmark that I use 

----- cut here ----
#!/usr/local/bin/perl
# Benchmarking pattern matching
# Sun4 - Sun OS 4.1.1
#
print "Enter a pattern to grep : ";
$pat = <STDIN>; chop $pat;

# Using UNIX grep
$start = time;
open (FLAT,"grep $pat flatfile|");
while (<FLAT>) { print; }
$elapse = time - $start;
print ">>>> UNIX grep takes $elapse sec.\n";
close(FLAT);

# Using perl pattern matching
open(FLAT,"flatfile");
$start = time;
while (<FLAT>) {
    if (/$pat/) { print;}
    }
$elapse = time - $start;
print ">>>> Perl's pattern matching takes $elapse sec.\n";
close(FLAT);


