Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^2: Perl::Critic and Subroutines

by tobyink (Abbot)
on Nov 28, 2012 at 10:23 UTC ( #1006000=note: print w/replies, xml ) Need Help??

in reply to Re: Perl::Critic and Subroutines
in thread Perl::Critic and Subroutines

"I am not a fan of this policy :)"

There seems little point to it. There is a very slight compile time speed penalty to double-quoted strings. This benchmark demonstrates that:

use Benchmark ':all'; cmpthese(250_000, { e_double => sub { eval q{"foo"} }, e_single => sub { eval q{'foo'} }, });

But at runtime, there's no difference, as they compile to the same optree...

$ 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

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...

<?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";'), ));
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^3: Perl::Critic and Subroutines
by jthalhammer (Friar) on Dec 04, 2012 at 06:17 UTC

    "I am not a fan of this policy :)"

    There seems little point to it.

    I actually think this is one of the best policies. As you pointed out, the performance benefits are very small. But it is immensely helpful for clarifying intent and detecting typos. Consider this code:

    my $name = <STDIN>; print "Hello, name";

    Unless you had test coverage on that (or you just spotted it by eye) you'd never find that bug. But ProhibitUselessInterpolation will find it for you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006000]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2017-01-19 18:27 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.