Some Perl Speed-up tips
Use constants rather than regexes when splitting
The following was run on a large text file (TWiki.pm, to be exact).
my $n = 1000;
my @start = times();
while ($n--) {
$text =~ tr/\r//d;
split("\n", $text);
}
my @end = times();
my $total = @end[0] - @start[0];
print "Total time for simple split: $total\n";
my $n = 1000;
@start = times();
while ($n--) {
split(/\r?\n/, $text);
}
my @end = times();
my $total = @end[0] - @start[0];
print "Total time for regex split: $total\n";
The results? 7 seconds for the simple split, 10 seconds for the regex split. (The file did not, in fact, have any \r characters in it, for what that's worth).
--
Contributors: MeredithLesly
Discussion
--
MeredithLesly - 21 Jul 2006