Foodeywo has asked for the wisdom of the Perl Monks concerning the following question:
today I re-stumbled upon an issue I quick-and-dirty solved a while ago but I want to solve it more elegantly while I am doing code polishing these days.
I use Regexp::Assemble to assemble regex that are about 15kb to 87kb large. Now I very simply run through a large (~10GB) file and match the regex. I used to do this on the command line in the style
perl -ne 'print if (/MYLARGEREGEX_HERE/../END_OF_BLOCK/)' inputfile > +outputfile
this was fast as hell. However when my regex grew in size, I was not able to copy paste them into the bash so I started to read the regex from a file and did something like this
#!perl use strict; use warnings; open my $fh_big_file, '<', $ARGV[0] || die; #first argument must be th +e input file open my $fh_regex, '<', $ARGV[1] || die; # second argument points to t +he file containing the regex my $regex = <$fh_regex>; while(<$fh_big_file>) { print if (/$regex/../^END_OF_BLOCK/); }
The funny thing is, that this flavour of code costs me factor 20 in speed or even more. I can reclaim the speed by avoiding to store the regex in a variable, e.g.
while(<$fh_big_file>) { print if (/MY_HUGE_REGEX_JUST_PLAIN/../^END_OF_BLOCK/); }
so I assume this has something to do with fetching the content of the variable (from RAM to CPU?) over and over for each loop of while(<>), whereas inputing the regex directly doesnt need to re-read it every time.
This approach however requires me to manually copy the regex to its place each time I run the whole procedure of "assembling, searching, processing, assembling, seachring, processing" and I would like to automize it without loss of performance. Any ideas?
thanks and cheers!
Update/Solution/Close
The suggestion to use the o operator works. However it needs to be behind /$regex/ not behind /END_OF_BLOCK/. i.e. like shmem suggested:
while(<$fh_big_file>) { print if (/$regex/o .. /^END_OF_BLOCK/); }
thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Performance of assambled regex
by Not_a_Number (Prior) on Jul 26, 2015 at 18:22 UTC | |
|
Re: Performance of assambled regex
by BillKSmith (Monsignor) on Jul 26, 2015 at 17:28 UTC | |
|
Re: Performance of assambled regex
by Laurent_R (Canon) on Jul 26, 2015 at 12:52 UTC | |
by shmem (Chancellor) on Jul 26, 2015 at 12:58 UTC | |
|
Re: Performance of assambled regex
by shmem (Chancellor) on Jul 26, 2015 at 12:52 UTC | |
|
Re: Performance of assambled regex
by natan (Novice) on Jul 27, 2015 at 09:43 UTC | |
|
Re: Performance of assambled regex
by anonymized user 468275 (Curate) on Jul 27, 2015 at 13:42 UTC |