Skip to content

Instantly share code, notes, and snippets.

@rybakit
Last active December 12, 2015 07:38
Show Gist options
  • Save rybakit/4737807 to your computer and use it in GitHub Desktop.
Save rybakit/4737807 to your computer and use it in GitHub Desktop.
array_replace() vs array union benchmark
<?php
for ($i = 0; $i < 1000000; ++$i) {
$server = $_SERVER;
$server += array(
'SERVER_NAME' => 'localhost',
'SERVER_PORT' => 80,
'HTTP_HOST' => 'localhost',
'HTTP_USER_AGENT' => 'Symfony/2.X',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'REMOTE_ADDR' => '127.0.0.1',
'SCRIPT_NAME' => '',
'SCRIPT_FILENAME' => '',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_TIME' => time(),
);
}
<?php
for ($i = 0; $i < 1000000; ++$i) {
$server = $_SERVER;
$server = array_replace(array(
'SERVER_NAME' => 'localhost',
'SERVER_PORT' => 80,
'HTTP_HOST' => 'localhost',
'HTTP_USER_AGENT' => 'Symfony/2.X',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'REMOTE_ADDR' => '127.0.0.1',
'SCRIPT_NAME' => '',
'SCRIPT_FILENAME' => '',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_TIME' => time(),
), $server);
}
php -v
PHP 5.4.6-1ubuntu1.1 (cli) (built: Nov 15 2012 01:18:34)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
time php array_replace.php
real 0m4.961s
user 0m4.948s
sys 0m0.000s
real 0m4.905s
user 0m4.888s
sys 0m0.004s
real 0m4.878s
user 0m4.856s
sys 0m0.008s
time php array_add.php
real 0m2.967s
user 0m2.948s
sys 0m0.008s
real 0m2.980s
user 0m2.972s
sys 0m0.004s
real 0m2.993s
user 0m2.980s
sys 0m0.004s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment