Hos*_*diq 4 php performance closures anonymous-function
今天早些时候我正在开发一个PHP 5.3+应用程序,这意味着我可以自由使用PHP闭包.太棒了,我想!然后我遇到了一段代码,其中使用功能性PHP代码会使事情变得更容易,但是,虽然我有一个合乎逻辑的答案,但它让我想知道直接调用封闭内容array_map()和传递它之间的性能影响是什么作为一个变量.即以下两个:
$test_array = array('test', 'test', 'test', 'test', 'test' );
array_map( function ( $item ) { return $item; }, $test_array );
Run Code Online (Sandbox Code Playgroud)
和
$test_array = array('test', 'test', 'test', 'test', 'test' );
$fn = function ( $item ) { return $item; };
array_map( $fn, $test_array );
Run Code Online (Sandbox Code Playgroud)
正如我所想,后者确实更快,但差别并不大.实际上,重复这些相同的测试10000次并取平均值的差异为0.05秒.甚至可能是侥幸.
这让我更加好奇.怎么样create_function()和关闭?同样,经验告诉我,create_function()当它array_map()创建一个函数,评估它,然后存储它时,它应该更慢.而且,正如我所想的那样,create_function()确实更慢.这就是全部array_map().
然后,我不知道为什么我这样做,但是我做了,我检查了create_function()保存它之间的差异和闭包之间的区别.没有处理,没有任何东西,只是简单地传递一个字符串,并返回该字符串.
测试成了:
$fn = function($item) { return $item; };
$fn('test');
Run Code Online (Sandbox Code Playgroud)
和
$fn = create_function( '$item', 'return $item;' );
$fn('test');
Run Code Online (Sandbox Code Playgroud)
我分别运行了10000次这些测试,并查看了结果并获得了平均值.我对结果感到非常惊讶.
事实证明,此次关闭的速度约为4倍.这不是我想的.我的意思是,通过一个闭包运行array_map()速度要快得多,并且通过变量运行相同的函数array_map()甚至更快,这实际上与此测试相同.
结果是
array
0 =>
array
'test' => string 'Closure test' (length=12)
'iterations' => int 10000
'time' => float 5.1327705383301E-6
1 =>
array
'test' => string 'Anonymous test' (length=14)
'iterations' => int 10000
'time' => float 1.6745710372925E-5
Run Code Online (Sandbox Code Playgroud)
好奇为什么它这样做,我检查了CPU使用率和其他系统资源,并确保没有必要运行,一切都很好,所以我再次运行测试,但我得到了类似的结果.
所以我只尝试了一次相同的测试,然后多次运行(当然每次都计时).事实证明闭合确实慢了4倍,除了偶尔会比create_function()它快两到三倍,我猜它只是吸吮,但它似乎足以将时间减少一半我做了1000次测试.
下面是我用来进行这些测试的代码.谁能告诉我这到底是怎么回事?这是我的代码还是只是PHP代理?
<?php
/**
* Simple class to benchmark code
*/
class Benchmark
{
/**
* This will contain the results of the benchmarks.
* There is no distinction between averages and just one runs
*/
private $_results = array();
/**
* Disable PHP's time limit and PHP's memory limit!
* These benchmarks may take some resources
*/
public function __construct() {
set_time_limit( 0 );
ini_set('memory_limit', '1024M');
}
/**
* The function that times a piece of code
* @param string $name Name of the test. Must not have been used before
* @param callable|closure $callback A callback for the code to run.
* @param boolean|integer $multiple optional How many times should the code be run,
* if false, only once, else run it $multiple times, and store the average as the benchmark
* @return Benchmark $this
*/
public function time( $name, $callback, $multiple = false )
{
if($multiple === false) {
// run and time the test
$start = microtime( true );
$callback();
$end = microtime( true );
// add the results to the results array
$this->_results[] = array(
'test' => $name,
'iterations' => 1,
'time' => $end - $start
);
} else {
// set a default if $multiple is set to true
if($multiple === true) {
$multiple = 10000;
}
// run the test $multiple times and time it every time
$total_time = 0;
for($i=1;$i<=$multiple;$i++) {
$start = microtime( true );
$callback();
$end = microtime( true );
$total_time += $end - $start;
}
// calculate the average and add it to the results
$this->_results[] = array(
'test' => $name,
'iterations' => $multiple,
'time' => $total_time/$multiple
);
}
return $this; //chainability
}
/**
* Returns all the results
* @return array $results
*/
public function get_results()
{
return $this->_results;
}
}
$benchmark = new Benchmark();
$benchmark->time( 'Closure test', function () {
$fn = function($item) { return $item; };
$fn('test');
}, true);
$benchmark->time( 'Anonymous test', function () {
$fn = create_function( '$item', 'return $item;' );
$fn('test');
}, true);
$benchmark->time( 'Closure direct', function () {
$test_array = array('test', 'test', 'test', 'test', 'test' );
$test_array = array_map( function ( $item ) { return $item; }, $test_array );
}, true);
$benchmark->time( 'Closure stored', function () {
$test_array = array('test', 'test', 'test', 'test', 'test' );
$fn = function ( $item ) { return $item; };
$test_array = array_map( $fn, $test_array );
}, true);
$benchmark->time( 'Anonymous direct', function () {
$test_array = array('test', 'test', 'test', 'test', 'test' );
$test_array = array_map( create_function( '$item', 'return $item;' ), $test_array );
}, true);
$benchmark->time( 'Anonymous stored', function () {
$test_array = array('test', 'test', 'test', 'test', 'test' );
$fn = create_function( '$item', 'return $item;' );
$test_array = array_map( $fn, $test_array );
}, true);
var_dump($benchmark->get_results());
Run Code Online (Sandbox Code Playgroud)
以及此代码的结果:
array
0 =>
array
'test' => string 'Closure test' (length=12)
'iterations' => int 10000
'time' => float 5.4110765457153E-6
1 =>
array
'test' => string 'Anonymous test' (length=14)
'iterations' => int 10000
'time' => float 1.6784238815308E-5
2 =>
array
'test' => string 'Closure direct' (length=14)
'iterations' => int 10000
'time' => float 1.5178990364075E-5
3 =>
array
'test' => string 'Closure stored' (length=14)
'iterations' => int 10000
'time' => float 1.5463256835938E-5
4 =>
array
'test' => string 'Anonymous direct' (length=16)
'iterations' => int 10000
'time' => float 2.7537250518799E-5
5 =>
array
'test' => string 'Anonymous stored' (length=16)
'iterations' => int 10000
'time' => float 2.8293371200562E-5
Run Code Online (Sandbox Code Playgroud)
use*_*008 19
5.1327705383301E-6不比1.6745710372925E-5慢4倍; 它快3倍左右.你正在读错号码.似乎在你的所有结果中,闭包始终比...更快create_function.
| 归档时间: |
|
| 查看次数: |
2484 次 |
| 最近记录: |