Last active
December 10, 2015 22:48
-
-
Save deekayen/4504581 to your computer and use it in GitHub Desktop.
Test the speed of two different ways to quote a PHP string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Test quote speeds. | |
*/ | |
$runs = 100000000; | |
class Timer { | |
protected $last, $timers = array(), $length = 10; | |
public function start() { | |
$this->last = microtime(true); | |
} | |
public function mark($name) { | |
$this->timers[$name] = microtime(true) - $this->last; | |
$this->last = microtime(true); | |
$this->length = max(strlen($name) + 2, $this->length); | |
} | |
public function display($runs) { | |
printf("%-{$this->length}s: %s" . PHP_EOL, 'Iterations', number_format($runs)); | |
foreach ($this->timers as $name => $value) printf("%-{$this->length}s: %.3f secs" . PHP_EOL, $name, $value); | |
} | |
} | |
$timer = new Timer; | |
$timer->start(); | |
// ******************************************************************* | |
for ($i = 0; $i < $runs; $i++) { $var = '\'STORE_CLOSE_DATE\' is added via prepareRow().'; } | |
$timer->mark('Escaped singles'); | |
// ******************************************************************* | |
for ($i = 0; $i < $runs; $i++) { $var = "'STORE_CLOSE_DATE' is added via prepareRow()."; } | |
$timer->mark('Un-escaped doubles'); | |
$timer->display($runs); |
Iterations : 100,000,000 Escaped singles : 7.663 secs Un-escaped doubles : 7.572 secs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Iterations : 1,000,000 Escaped singles : 0.077 secs Escaped doubles : 0.075 secs