Next | Making Programs Faster | 28 |
substr($s, 0, 3) = "abc"
$s =~ s/.../abc/s
Which is faster?
my $N = shift || 1000000; my $s = shift || "The quick brown fox jumps over the lazy dog";
my ($su, $ss) = times; for (1 .. $N) { substr($s, 0, 3) = "abc" } my ($eu, $es) = times; my ($tu, $ts) = ($eu - $su, $es - $ss); my $total = $tu + $ts; printf "%20s %5.2f %5.2f %6.2f\n", "substr", $tu, $ts, $total;
my ($su, $ss) = times; for (1 .. $N) { $s =~ s/.../abc/s } my ($eu, $es) = times; my ($tu, $ts) = ($eu - $su, $es - $ss); my $total = $tu + $ts; printf "%20s %5.2f %5.2f %6.2f\n", "regex", $tu, $ts, $total;
The output:
substr 5.04 0.01 5.05 regex 5.71 0.00 5.71
Next | Copyright © 2003 M. J. Dominus |