Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ncar!gatech!usenet.ins.cwru.edu!ncoast!allbery From: allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/AA) Newsgroups: comp.lang.perl Subject: Re: pattern matching performance Message-ID: <1991May6.225810.20985@NCoast.ORG> Date: 6 May 91 22:58:10 GMT References: <1991May6.161906.21161@ccu.umanitoba.ca> Reply-To: allbery@ncoast.ORG (Brandon S. Allbery KB8JRR/AA) Followup-To: comp.lang.perl Organization: North Coast Public Access Un*x (ncoast) Lines: 27 As quoted from <1991May6.161906.21161@ccu.umanitoba.ca> by rahardj@ccu.umanitoba.ca (Budi Rahardjo): +--------------- | 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. | | while () { | if (/$pat/) { print;} | } +--------------- The problem is that /$pat/ gets recompiled for every line. If you never change the value of $pat, the sequence /$pat/o will be compiled only once. If you plan to change it occasionally, you need to be a bit sneakier: eval 'sub match { print if /$pat/o; }'; Run this every time you change the value of $pat, then call &match to do the comparison. (Since the line is already in $_, just say "&match;" to make it even faster.) (I think.) ++Brandon -- Me: Brandon S. Allbery Ham: KB8JRR/AA 10m,6m,2m,220,440,1.2 Internet: allbery@NCoast.ORG (restricted HF at present) Delphi: ALLBERY AMPR: kb8jrr.AmPR.ORG [44.70.4.88] uunet!usenet.ins.cwru.edu!ncoast!allbery KB8JRR @ WA8BXN.OH