note
tobyink
<blockquote><p><i>
"I am not a fan of this policy :)"
</i></p></blockquote>
<p>There seems little point to it. There is a very slight compile time speed penalty to double-quoted strings. This benchmark demonstrates that:</p>
<c>
use Benchmark ':all';
cmpthese(250_000, {
e_double => sub { eval q{"foo"} },
e_single => sub { eval q{'foo'} },
});
</c>
<p>But at runtime, there's no difference, as they compile to the same optree...</p>
<c>
$ perl -MO=Concise -e'$x=q{foo}' > single.txt
-e syntax OK
$ perl -MO=Concise -e'$x=qq{foo}' > double.txt
-e syntax OK
$ diff -s single.txt double.txt
Files single.txt and double.txt are identical
$ rm single.txt double.txt
</c>
<p>That is not always the case in all programming languages though. The PHP compiler is less smart than the Perl compiler, so in PHP there's a real speed penalty on double-quoted strings...</p>
<readmore><code>
<?php
class Bench {
public $name;
protected $_start;
protected $_finish;
public function __construct ($name) {
$this->name = $name;
}
public function start () {
if ($this->_start) die("already started");
$this->_start = microtime(true);
}
public function finish () {
if ($this->_finish) die("already finished");
$this->_finish = microtime(true);
}
public function total_time () {
return $this->_finish - $this->_start;
}
public function run ($callable, $count=1) {
$this->start();
for ($i = 0; $i < $count; $i++)
call_user_func($callable);
$this->finish();
}
public function __toString () {
return sprintf("%s: %0.6f s", $this->name, $this->total_time());
}
public static function cmpthese ($iterations, $tests) {
foreach ($tests as $name => $code) {
$bench = new Bench ($name);
$bench->run($code, $iterations);
$per_second = $iterations / $bench->total_time();
printf("%s: %0.2f /s\n", $bench->name, $per_second);
}
}
}
Bench::cmpthese(100000, array(
"single quotes" => create_function('', 'return \'foo\';'),
"double quotes" => create_function('', 'return "foo";'),
));
</code></readmore>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-757127">
<small><small>
<tt>perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
</tt></small></small>
</div></div>
1005926
1005933