You are right, and thanks for pointing out. My original analysis took the assumption that both s/// and split iterate through the sentence with the same performance, however that was wrong, and split() is much slower:
use strict;
use warnings;
use Benchmark qw (:all);
my $txt = "a" x 100;
sub seperate {
split //, $txt;
}
sub replace {
$txt =~ s/a/b/g;
}
my $result = timethese(100000, {'seperate' => \&seperate, 'replace' =>
+ \&replace});
This gives:
Benchmark: timing 10000 iterations of replace, seperate...
replace: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
(warning: too few iterations for a reliable count)
seperate: 2 wallclock secs ( 1.20 usr + 0.00 sys = 1.20 CPU) @ 83
+05.65/s (n
=10000)
C:\Perl\bin>perl -w math1.pl
Benchmark: timing 100000 iterations of replace, seperate...
replace: 1 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU) @ 31
+25000.00/s
(n=100000)
(warning: too few iterations for a reliable count)
seperate: 16 wallclock secs (12.50 usr + 0.00 sys = 12.50 CPU) @ 80
+00.00/s (n
=100000)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|