A quicky test shows that 3-arg substr is faster than chop, but a
4-arg substr is even faster - and that's to be expected because it saves a copy. Differences aren't dramatic though.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw /cmpthese/;
chomp (our @strings = <DATA>);
cmpthese -5 => {
chop => 'my @copy = @strings;
foreach my $str (@copy) {
chop $str;
chop $str;
chop $str;
chop $str;
}',
substr3 => 'my @copy = @strings;
foreach my $str (@copy) {
$str = substr $str => 0, -4;
}',
substr4 => 'my @copy = @strings;
foreach my $str (@copy) {
substr $str => -4, 4 => "";
}',
};
__DATA__
asdfjas;dfjas;dfjas;dkfjasdfasdf
asd
sdfajsd;flaks
Rate chop substr3 substr4
chop 152166/s -- -11% -14%
substr3 170187/s 12% -- -4%
substr4 176800/s 16% 4% --
-
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.
|