in reply to Re^3: Using regex with a variable
in thread Using regex with a variable
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Using regex with a variable
by Discipulus (Canon) on Mar 14, 2016 at 09:43 UTC | |
Whitout this it is impossible to guess what you are triyng to achieve. Probably a single regex can do the job you need. Here around are very good regexer (not me). Do not modify your original post without putting some note on changes: replies can have no sense after the modification of the original post. 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. | [reply] [d/l] |
by Praveen Dharmaraj (Novice) on Mar 15, 2016 at 05:18 UTC | |
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.
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.
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. | [reply] [d/l] [select] |
by Discipulus (Canon) on Mar 15, 2016 at 08:25 UTC | |
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
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 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. | [reply] [d/l] [select] |
by Praveen Dharmaraj (Novice) on Mar 18, 2016 at 05:37 UTC | |
by Praveen Dharmaraj (Novice) on Mar 15, 2016 at 10:13 UTC | |
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. I'm making a couple of assumptions:
So your final code might look something like this (untested): (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: <%-{-{-{-< | [reply] [d/l] [select] |