Skip to content

Instantly share code, notes, and snippets.

@bar
Created November 5, 2012 02:45
Show Gist options
  • Save bar/4015025 to your computer and use it in GitHub Desktop.
Save bar/4015025 to your computer and use it in GitHub Desktop.
http_build_query() vs. buildQuery() - 100000 iterations (3 passes)
/**
* 100000 iterations x pass (3 passes)
* Router::queryString() uses http_build_query()
* Router::queryString2() uses buildQuery()
* Router::url() uses Router::queryString()
* Router::url2() uses Router::queryString2()
*/
/**
* Router::url()
* #1 http_build_query: 38.556071043015
* #2 http_build_query: 37.981322050095
* #3 http_build_query: 38.211707115173
* #1 buildQuery: 42.149253129959
* #2 buildQuery: 41.718835830688
* #3 buildQuery: 41.908570051193
*/
public function testRouterUrl2() {
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')));
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data')));
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data')));
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')), array('escape' => true));
}
echo PHP_EOL . 'http_build_query: ' . (microtime(true) - $start);
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$result = Router::url2(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
$result = Router::url2(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')));
$result = Router::url2(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data')));
$result = Router::url2(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data')));
$result = Router::url2(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')), array('escape' => true));
}
echo PHP_EOL . 'buildQuery: ' . (microtime(true) - $start);
}
/**
* Router::queryString()
* #1 http_build_query: 2.7396218776703
* #2 http_build_query: 2.7326550483704
* #3 http_build_query: 2.709762096405
* #1 buildQuery: 6.8002860546112
* #2 buildQuery: 6.9906620979309
* #3 buildQuery: 6.7362649440765
*/
public function testRouterQueryString2() {
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$result = Router::queryString('?apple=red&pear=green', array('some' => 'param', 'foo' => 'bar'));
$result = Router::queryString('apple=red&pear=green');
$result = Router::queryString('foo=bar', array('php' => 'nut', 'jose' => 'zap'), true);
$result = Router::queryString('foo=bar&amp;', array('php' => 'nut', 'jose' => 'zap'), true);
$result = Router::queryString('foo=bar&', array('php' => 'nut', 'jose' => 'zap'));
$result = Router::queryString(array('apple' => 'red', 'pear' => 'green'), array('some' => 'param', 'foo' => 'bar'));
}
echo PHP_EOL . 'http_build_query: ' . (microtime(true) - $start);
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$result = Router::queryString2('?apple=red&pear=green', array('some' => 'param', 'foo' => 'bar'));
$result = Router::queryString2('apple=red&pear=green');
$result = Router::queryString2('foo=bar', array('php' => 'nut', 'jose' => 'zap'), true);
$result = Router::queryString2('foo=bar&amp;', array('php' => 'nut', 'jose' => 'zap'), true);
$result = Router::queryString2('foo=bar&', array('php' => 'nut', 'jose' => 'zap'));
$result = Router::queryString2(array('apple' => 'red', 'pear' => 'green'), array('some' => 'param', 'foo' => 'bar'));
}
echo PHP_EOL . 'buildQuery: ' . (microtime(true) - $start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment