pbeckingham has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to benchmark string concatenation. I am trying to compare string interpolation to string concatenation when they are used to do the same thing. My suspicion is that non-interpolated concatenation beats interpolation, and I have been told that that is true. So I wrote this to test the claim:
Here are the results I get:#! /usr/bin/perl -w use strict; use Benchmark qw(:all); my $sample = 'abcdefghijklmnopqrstuvwxyz'; sub interpolated () { my $s = ''; for (0 .. 99) {$s = "$s$sample"} } sub noninterpolated () { my $s = ''; for (0 .. 99) {$s .= $sample} } cmpthese (-1, {interp => "interpolated()", nonint => "noninterpolated()"});
Now these results show practically identical numbers, and I believe that they should be more disparate and that my tests are inadequate. So my question is, what can I do to improve my tests? Should I leave the iteration to the cmpthese routine? Am I testing in a way that gets optimized down to identical code?Rate nonint interp nonint 13917/s -- -0% interp 13964/s 0% --
Thank you all.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: String Concatenation Performance
by chromatic (Archbishop) on Mar 20, 2004 at 17:05 UTC | |
by pbeckingham (Parson) on Mar 20, 2004 at 17:56 UTC | |
by Aristotle (Chancellor) on Mar 20, 2004 at 18:24 UTC | |
|
Re: String Concatenation Performance
by TomDLux (Vicar) on Mar 20, 2004 at 15:49 UTC | |
by pbeckingham (Parson) on Mar 20, 2004 at 16:05 UTC | |
by TomDLux (Vicar) on Mar 20, 2004 at 16:57 UTC | |
by pbeckingham (Parson) on Mar 20, 2004 at 17:10 UTC | |
|
Re: String Concatenation Performance
by Aristotle (Chancellor) on Mar 20, 2004 at 18:26 UTC | |
by pbeckingham (Parson) on Mar 20, 2004 at 19:00 UTC | |
by TimToady (Parson) on Mar 20, 2004 at 23:13 UTC |