
Laurent_R
Veteran
/ Moderator
Nov 15, 2012, 1:23 AM
Post #2 of 6
(15452 views)
|
Re: [anujajoseph] Need help to fine tune perl script to make it faster( currently taking more than 30 minutes)
[In reply to]
|
Can't Post
|
|
Hi, there are many things to say about your code not respecting the good practices that are currently generally accepted. The main thing being that you should always "use strict;" and "use warnings;" pragmas in any Perl script that is longer that 2 or 3 lines. This will force you to declare your variable in appropriate scope, but it will help you find bugs or possible misconstructs. Havins said that, none of these good practices is likely to speed up your program in any significant way. Generally speaking, having looked quickly at your code, I haven't any obviously utterly unefficient construct. If you are running a (very) old version of Perl, it could be that replacing:
{ $_ =~ s/$str1/$str2/g; $_ =~ s/$str3/$str4/g; by
{ $_ =~ s/$str1/$str2/go; $_ =~ s/$str3/$str4/go; will noticeably improve performance because, on old version of Perl (before Perl 5.6), the interpreter would have to recompile the regex for each line of input, whereas the /o option tells the interpreter to always reuse the compiled form of the regex. If you are running a recent version of Perl this will most probably not make any difference. BTW, if you are running a recent version of Perl, you should have a look at the qr// solution for declaring regular expression. There is something that might possibly improve the two lines above. Since you are not really using pattern matching, it is a bit of an overkill to use the regex engine for achiving something that can be done with simpler sttring recognition. So you may try to replace your two expressions quoted above by a combination of the index (to find the place) and the substr (to perform the string substitution) functions. This may or may not be faster, only benchmarking the two alternatives will tell you for sure. Actually, if you do it, I would be interested in knowing the results. These are just some possible clues, but don't expect a huge improvement from these and read on, I am getting to the most important point. The key to improving performance is to profile your code, to figure out where the program is spending a lot of time. Often, 20% of the code account for 80% of the time spent. If you improve sections of the code not belong ging to those 20% that are slow, you will not get any significant improvement (and you are probably wasting your time). In addition, it is generally admitted that programmers are often bad at guessing where the slow part is. Therefore using a profiling tool is really the most important thing you can do, as a starting point, for improving your program performance (of course it is very important to do this code profiling on actual data, otherwise your results may be unsignificant). Most profilers, however, have a function-level granurality. Since you don't have any function, they would be useless in your case. You should probably try the CPAN's Devel::NYTProf module, which works at the line of code level. For more details on improving program performance, take a look at the following document: http://search.cpan.org/~dom/perl-5.12.5/pod/perlperf.pod.
|