Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Скорость выполнения call_user_func_array() на разных версиях PHP
<?php
/**
* @link http://ukrdev.com.ua/ru/question/1074/call-user-func-array---skorost-vipolneniya.html
* @link https://www.garfieldtech.com/blog/magic-benchmarks
*/
$args = [
['image/gif'],
['image/jpeg', 'image/pjpeg'],
['image/png', 'image/x-png'],
['image/bmp', 'image/x-ms-bmp', 'image/x-bmp'],
['image/tiff', 'image/tiff-fx'],
];
function fn($args)
{
return array_merge(...$args);
}
class A
{
public function __invoke($args)
{
return array_merge(...$args);
}
}
class B
{
public function fn($args)
{
return array_merge(...$args);
}
}
// +--------------------------------------------------+----------+----------+----------+-----------+
// | |PHP 5.6.30|PHP 7.0.24|PHP 7.1.10|HHVM 3.22.0|
// +--------------------------------------------------+----------+----------+----------+-----------+
// |array_merge(...$args) |0.9381 |0.2910 |0.2945 |0.7795 |
// |call_user_func_array('array_merge', $args) |1.4194 |0.3073 |0.2938 |1.0372 |
// +--------------------------------------------------+----------+----------+----------+-----------+
// |fn($args) |1.0934 |0.6369 |0.4660 |1.2059 |
// |$fn($args) |1.4507 |0.5399 |0.6090 |1.2501 |
// |$class->fn($args) |1.5088 |0.5622 |0.5282 |1.4568 |
// |call_user_func_array('fn', [$args]) |1.5022 |0.4198 |0.3750 |1.2159 |
// |call_user_func_array([new A, '__invoke'], [$args])|1.9369 |0.5629 |0.6538 |1.3701 |
// |call_user_func_array([new B, 'fn'], [$args]) |2.1243 |0.7088 |0.6625 |1.9792 |
// +--------------------------------------------------+----------+----------+----------+-----------+
/**
* @see https://3v4l.org/KWij4
*
* +-----------+--------+
* |Version |Speed |
* +-----------+--------+
* |PHP 5.6.30 |0.9381 s|
* |PHP 7.0.24 |0.2910 s|
* |PHP 7.1.10 |0.2945 s|
* |HHVM 3.22.0|0.7795 s|
* +-----------+--------+
*/
for ($i = 0; $i < 1000000; $i++) {
array_merge(...$args);
}
/**
* @see https://3v4l.org/0ciu4
*
* +-----------+--------+
* |Version |Speed |
* +-----------+--------+
* |PHP 5.6.30 |1.4194 s|
* |PHP 7.0.24 |0.3073 s|
* |PHP 7.1.10 |0.2938 s|
* |HHVM 3.22.0|1.0372 s|
* +-----------+--------+
*/
for ($i = 0; $i < 1000000; $i++) {
call_user_func_array('array_merge', $args);
}
/**
* @see https://3v4l.org/WAb0b
*
* +-----------+--------+
* |Version |Speed |
* +-----------+--------+
* |PHP 5.6.30 |1.0934 s|
* |PHP 7.0.24 |0.6396 s|
* |PHP 7.1.10 |0.4660 s|
* |HHVM 3.22.0|1.2059 s|
* +-----------+--------+
*/
for ($i = 0; $i < 1000000; $i++) {
fn($args);
}
/**
* @see https://3v4l.org/olQU2
*
* +-----------+--------+
* |Version |Speed |
* +-----------+--------+
* |PHP 5.6.30 |1.4507 s|
* |PHP 7.0.24 |0.5399 s|
* |PHP 7.1.10 |0.6090 s|
* |HHVM 3.22.0|1.2501 s|
* +-----------+--------+
*/
for ($i = 0; $i < 1000000; $i++) {
$fn = new A;
$fn($args);
}
/**
* @see https://3v4l.org/59e1L
*
* +-----------+--------+
* |Version |Speed |
* +-----------+--------+
* |PHP 5.6.30 |1.5088 s|
* |PHP 7.0.24 |0.5622 s|
* |PHP 7.1.10 |0.5282 s|
* |HHVM 3.22.0|1.4568 s|
* +-----------+--------+
*/
for ($i = 0; $i < 1000000; $i++) {
$class = new B;
$class->fn($args);
}
/**
* @see https://3v4l.org/8t9mA
*
* +-----------+--------+
* |Version |Speed |
* +-----------+--------+
* |PHP 5.6.30 |1.5022 s|
* |PHP 7.0.24 |0.4198 s|
* |PHP 7.1.10 |0.3750 s|
* |HHVM 3.22.0|1.2159 s|
* +-----------+--------+
*/
for ($i = 0; $i < 1000000; $i++) {
call_user_func_array('fn', [$args]);
}
/**
* @see https://3v4l.org/ucIg4
*
* +-----------+--------+
* |Version |Speed |
* +-----------+--------+
* |PHP 5.6.30 |1.9369 s|
* |PHP 7.0.24 |0.5629 s|
* |PHP 7.1.10 |0.6538 s|
* |HHVM 3.22.0|1.3701 s|
* +-----------+--------+
*/
for ($i = 0; $i < 1000000; $i++) {
call_user_func_array([new A, '__invoke'], [$args]);
}
/**
* @see https://3v4l.org/u8unN
*
* +-----------+--------+
* |Version |Speed |
* +-----------+--------+
* |PHP 5.6.30 |2.1243 s|
* |PHP 7.0.24 |0.7088 s|
* |PHP 7.1.10 |0.6625 s|
* |HHVM 3.22.0|1.9792 s|
* +-----------+--------+
*/
for ($i = 0; $i < 1000000; $i++) {
call_user_func_array([new B, 'fn'], [$args]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment