use Benchmark ':all';
cmpthese(250_000, {
e_double => sub { eval q{"foo"} },
e_single => sub { eval q{'foo'} },
});
####
$ 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
##
##
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";'),
));