Skip to content

Instantly share code, notes, and snippets.

@deekayen
Last active December 10, 2015 22:48
Show Gist options
  • Save deekayen/4504581 to your computer and use it in GitHub Desktop.
Save deekayen/4504581 to your computer and use it in GitHub Desktop.
Test the speed of two different ways to quote a PHP string.
<?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);
@deekayen
Copy link
Author

Iterations : 1,000,000 Escaped singles : 0.077 secs Escaped doubles : 0.075 secs

@deekayen
Copy link
Author

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