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.
In reply to String Concatenation Performance by pbeckingham
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |