in reply to Re^5: Using regex with a variable
in thread Using regex with a variable

Hello,

First of all, sorry for providing insufficient information about my problem. I understand now, that I need to explain my problem clearly for me to get some help.

OK. Here goes..

I am trying to find a few strings (stored in array) from about 50,000 lines of code in a file. After finding, I'm doing some changes to the matched strings and writing it to another file. To accomplish this task, I'm assigning 5 regexes to five variables.

foreach my $ur (@strings_to_be_matched) { $reg1 = qr/\=/i; $reg2 = qr/\S+\=\S+/i; $reg3 = qr/extern.+\b$ur\b\s*/i; $reg4 = qr/;$/i; $reg5 = qr/.+\b$ur\b\s*/i; foreach my $line (@contents_of_file) { if(($ln =~ $reg3 and $ln =~ $reg4)){ <some code> } if(($ln =~ $reg5 and $ln=~ $reg4 and ($ln !~ $reg1 or $ln =~ $reg +2)) { <some code> } if(($ln =~ $reg3 and $ln !~ $reg4)){ <some code> } if(($ln =~ $reg5 and $ln !~ $reg4 and $ln !~ $reg1)) { <some code> } } }

I'm using "qr" to pre-compile my regexes, so that I can reduce the time taken. I feel $reg5 is causing the problem here. The if conditions which involve $reg5 take longer time to match.

if (($ln =~ $reg5 and $ln=~ $reg4 and ($ln !~ $reg1 or $ln =~ $reg2))) { <some code here...> } if (($ln =~ $reg5 and $ln !~ $reg4 and $ln !~ $reg1)) { <some code here...> }

Another point to be noted is that, there are a few lines in the file which have a length of about ~12000. These lines take 4 seconds each to be processed.I want to reduce the time taken as few seconds taken to process each line are adding up to ~30 minutes. This is not acceptable at all in my case.

Replies are listed 'Best First'.
Re^7: Using regex with a variable
by Discipulus (Canon) on Mar 15, 2016 at 08:25 UTC
    Ok now is much clear, if your last post were the first one you would got more chance to get better replies.

    Anyway I have some sparse hints you can try to speed up your code. First you are doing something like for 5 regex, for 50k lines that is very heavy approach: you are processing every line 5 times. Normally when you iterate over a file (with the slowness of filesystem) is better to do the opposite: for 50k lines, for 5 regex (even if the result of 5 * 50k and 50k * 5 is identic) and you do not really need the big array at all!

    You are populating the array with a big amount of lines. This consume memory and slow your program. See reading large file where is explained that foreach my $line (<$readHandle>) {.. is list context and while (defined( my $line= <$readHandle>)) { is scalar context.

    So you need a change in the loop, like

    while (defined( my $ln= <$readHandle>)) { ... }

    After this i see you are using if .. if .. if .. are you sure this is your intention? or better if .. elsif.. elsif .. else.. you really want to match more than one case?

    Finally to speed up the loop you can insert some exit (from the loop) condition earlier in the loop. If this is possible is always worth to do.

    Also if you can anchor somehow your regexes this will speed up a lot the match in long lines.

    You can find these thread also interesting:

    Optimising large files processing
    Recommendations for efficient data reduction/substitution application
    Using indexing for faster lookup in large file

    HtH

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hi Discipulus,

      Thanks for your suggestions.

      I have now done three changes to reduce the time taken.

      1. Precompiled the regex and used them. This reduced the time taken from ~30 minutes to ~15 minutes. 2. Changed foreach loops to while loops 3. I managed to find the lines in input file which were responsible for longer runtime. I skipped parsing those lines, after confirming that they are not required. These lines were common in most of the input files.

      After all this, time to run has come down from ~30 minutes to ~35 seconds.

      A huge, huge, huge improvement.

      Solving this issue has gotten me more interested in perl. Cant thank you enough for your help!

      Hi,

      Thanks a lot for the suggestions. I will try them and update here the results :)

Re^7: Using regex with a variable
by AnomalousMonk (Archbishop) on Mar 15, 2016 at 16:41 UTC

    Another trick to reduce processing time is to compose all the  @strings_to_be_matched strings into a single regex.

    c:\@Work\Perl\monks>perl -wMstrict -le "my @strings_to_be_matched = qw(foo bar wibble wobble fee_fie foe fum) +; ;; my ($ur) = map qr{ $_ }xms, join q{ | }, map quotemeta, reverse sort @strings_to_be_matched ; print qq{\$ur: $ur}; ;; my $reg3 = qr/extern.+\b$ur\b\s*/i; print qq{\$reg3: $reg3}; " $ur: (?^msx: wobble | wibble | fum | foo | foe | fee_fie | bar ) $reg3: (?^i:extern.+\b(?^msx: wobble | wibble | fum | foo | foe | fee_ +fie | bar )\b\s*)
    I'm making a couple of assumptions:
    • the  @strings_to_be_matched strings are all C/C++ or similar keywords or identifiers and so consist entirely in  \w characters;
    • the  $ur pattern is always bounded by  \b assertions whenever it is interpolated.
    If these assumptions are true, then a couple of steps in creating the  $ur regex are redundant, but will do no harm.

    So your final code might look something like this (untested):

    my @strings_to_be_matched = ...; my ($ur) = map qr{ $_ }xms, join q{ | }, map quotemeta, reverse sort @strings_to_be_matched ; my $reg1 = qr/=/i; my $reg2 = qr/\S+=\S+/i; my $reg3 = qr/extern.+\b$ur\b\s*/i; my $reg4 = qr/;$/i; my $reg5 = qr/.+\b$ur\b\s*/i; foreach my $ln (@contents_of_file) { if ($ln =~ $reg3 and $ln =~ $reg4) { ... } if ($ln =~ $reg5 and $ln=~ $reg4 and ($ln !~ $reg1 or $ln =~ $reg2 +)) { ... } if ($ln =~ $reg3 and $ln !~ $reg4) { ... } if ($ln =~ $reg5 and $ln !~ $reg4 and $ln !~ $reg1) { ... } }
    (But please see Discipulus's remarks above about using a while-loop rather than a for-loop for processing file contents line-by-line.)

    Another thing that may affect speed is that you have all your regexes  $reg1 $reg2 $reg3 $reg4 $reg5 modified as  /i (case insensitive). Case insensitivity slows down regex execution. Some of your regexes have only assertions, characters or character classes  \S $ ; = to which case insensitivity does not apply. As noted above, the  @strings_to_be_matched strings seem to be C/C++ or suchlike keywords or identifiers; is case insensitivity ever appropriate here? I would seriously reconsider the use of case-insensitivity.

    Last but not least: As a beginner, it's important always to usewarnings; and usestrict; and avoid global variables.


    Give a man a fish:  <%-{-{-{-<