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

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.

Replies are listed 'Best First'.
Re^8: Using regex with a variable
by Praveen Dharmaraj (Novice) on Mar 18, 2016 at 05:37 UTC

    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!

Re^8: Using regex with a variable
by Praveen Dharmaraj (Novice) on Mar 15, 2016 at 10:13 UTC

    Hi,

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