Tags:
create new tag
view all tags

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

Topic revision: r1 - 2006-07-21 - MeredithLesly
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2026 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.