Re: Performance question with a string concat



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
.



Relevant Pages

  • Re: fastest count of instances in string?
    ... > and call that in the loop. ... then you should make all the benchmark ... but I can't figure how putting the tr// in a sub ... splitting the string and looking through the array). ...
    (comp.lang.perl.misc)
  • Re: QC Report - FastMM4 is Slow!!
    ... I do not see that piece of code as an example of string handling. ... I think that our benchmark suite should have such benchmarks and I think it ... most common situations. ...
    (borland.public.delphi.language.basm)
  • Re: STL Slow - VS2005
    ... accurately this benchmark represents real-world workloads. ... People have done quite a few studies on string lengths as they're ... quickly with length -- even 1000 characters is extremely rare. ... by reference, modify a string whose reference was passed. ...
    (microsoft.public.vc.stl)
  • Re: Benchmarking (Was: Re: Compiling Regexp only once)
    ... discounting loop overhead) (actual speedup in real code will probably be swallowed elsewhere or completely irrelevant). ... Constant lookup involves a bunch of C function calls, but literal lookup just returns an Object stored in the parse tree. ... Note that this is different from your benchmark in ruby-talk:204747, wherein much of the savings seems to come from not having to instantiate & populate a MatchData. ... N.times do string =~ /a c\s. ...
    (comp.lang.ruby)
  • Re: How to chck for string in array elements
    ... this is surprising (goes to show you, always benchmark :-)) ... sub reg { ... my $string = 'his'; ...
    (comp.lang.perl.misc)