Clear questions and runnable code
get the best and fastest answer
No More Meaningless Benchmarks!by chromatic (Archbishop)
|on Dec 08, 2006 at 21:24 UTC||Need Help??|
In Use of single quotes versus double quotes, the original poster asks "Which is faster, single quotes or double quotes?"
As you might expect, a couple of people responded with "It doesn't matter!" grinder followed up with more detail, explaining exactly why it doesn't matter to Perl. In this case, Perl compiles a non-interpolating double-quoted string and a single-quoted string (which never interpolates) into the exact same opcodes.
That is, Perl does the same work.
As you might expect, another monk tried to be helpful by pulling out Benchmark. Re: Use of single quotes versus double quotes argues that, on one machine, single-quoted constant non-interpolating strings are 16% slower than double-quoted strings.
Yet that's completely meaningless. Why? Because it has nothing to do with Perl! As grinder demonstrated, perl performs the same actions for each type of string. The differences are elsewhere in the system. It could be that one subroutine has ops all within one memory page and the other has less locality. Looking at the length of the strings, I suspect that there's a buffer size issue as well (IO screws up all benchmarks). Who knows if there are other processes running?
For fun, I started with a modified benchmark that runs for only five seconds and uses warn to send all output to STDERR, so I could redirect it to /dev/null. Here's my start:
... and the output:
What happened if I changed the order of subroutine declarations? That might change the memory layout:
The output is:
How about moving the declarations further?
There's one more code movement to try:
As expected, the results are unpredictable:
There are two possible morals to this story. The first is that moving code around can make your programs much faster. A better moral is know when benchmarks are useless.