Re: Performance question with a string concat
- From: Mike Heins <mikeh@xxxxxxxxxxxx>
- Date: Sat, 30 Apr 2005 14:44:29 -0000
On 2005-04-30, Simon von Janowsky <mail@xxxxxxxxxx> wrote:
> Hello,
> I want to let grow a string variable within a loop.
> A non-interpolated string '' is said to be faster than the usual "" string.
Not by much.
> Disadvantage of the non-interpolated string is that \n is not recognized.
>
> Now, what would be faster?
>
Why not try the Benchmark module and see?
use Benchmark;
my $code1 = sub {
my $out = '';
for(1 .. 1000) {
$out .= 'variable="value"' . "\n";
}
return $out;
};
my $code2 = sub {
my $out = '';
for(1 .. 1000) {
$out .= qq{variable="value"\n};
}
return $out;
};
use Benchmark;
timethese ( -5, {
SQUOTE => $code1,
DQUOTE => $code2,
});
Benchmark: running DQUOTE, JOIN, SQUOTE for at least 5 CPU seconds...
DQUOTE: 6 wallclock secs ( 5.34 usr + 0.00 sys = 5.34 CPU) @ 3495.51/s (n=18666)
SQUOTE: 6 wallclock secs ( 5.31 usr + 0.00 sys = 5.31 CPU) @ 3513.37/s (n=18656)
Benchmark: running DQUOTE, SQUOTE for at least 5 CPU seconds...
DQUOTE: 5 wallclock secs ( 5.26 usr + 0.00 sys = 5.26 CPU) @ 3484.22/s (n=18327)
SQUOTE: 6 wallclock secs ( 5.27 usr + 0.00 sys = 5.27 CPU) @ 3477.61/s (n=18327)
Looks like there is not a great difference. Perl has really gotten better
at string concats over the years -- at one point pushing the strings onto
an array and later doing a join() was competittive, and now it is not.
--
Mike Heins
Perusion -- Expert Interchange Consulting http://www.perusion.com/
Be patient. God isn't finished with me yet. -- unknown
.
- References:
- Performance question with a string concat
- From: Simon von Janowsky
- Performance question with a string concat
- Prev by Date: Re: multiples ifs
- Next by Date: Re: Performance question with a string concat
- Previous by thread: Re: Performance question with a string concat
- Next by thread: Re: Performance question with a string concat
- Index(es):
Relevant Pages
|