In PHP, some function take a "callable" as an argument, meaning you can specify a function to be executed at some point down the line. One example is array_map
.
在PHP中,一些函数将“callable”作为参数,这意味着您可以指定一个函数,该函数将在线的某一点执行。到就是一个例子。
PHP allows you to specify a callable in multiple ways, for example:
PHP允许您以多种方式指定可调用,例如:
// as a string:
$lowerCaseStrings = array_map('strtolower', $arrayOfStrings);
// object methods as an array
// (this could be done with DateTime directly, of course):
class DateFactory {
private $format;
public function __construct($format) {
$this->format = $format;
}
public function newDate($dateString) {
return DateTime::createFromFormat($this->format, $dateString);
}
}
$factory = new DateFactory('Y-m-d');
$dates = array_map(array($factory, 'newDate'), $arrayOfDateStrings);
// as a lambda expression / closure:
$dates = array_map(
function ($dateString) {
return DateTime::createFromFormat('Y-m-d', $dateString);
},
$arrayOfDateStrings
);
Now, I figure that the latter form gets evaluated by the PHP engine once during compilation, while the other might be evaluated during runtime, probably for each call, meaning using a closure would be far more efficient for a large number of calls. Is that assumption correct?
现在,我认为PHP引擎在编译期间会对后一个表单进行评估,而另一个表单可能会在运行时进行评估,可能是对每个调用进行评估,这意味着使用闭包对于大量调用来说效率要高得多。这个假设是正确的吗?
1 个解决方案
#1
3
I executed a function several different ways and recorded the run times. It looks like using a string reference to a regular function is much more efficient.
我以几种不同的方式执行了一个函数并记录了运行时间。看起来对常规函数使用字符串引用要高效得多。
Summary
总结
- Normal function definition with array_map(): 4.001193714
- 使用array_map()的法线函数定义:4.001193714
- Lambda definition outside loop with array_map(): 10.1116962
- 带array_map()的Lambda定义外循环:10.1116962
- Normal function definition and call: 3.208938837
- 普通函数的定义和调用:3.208938837
- Lambda definition and call within loop: 10.32323852
- λ定义和调用内循环:10.32323852
- Lambda definition outside loop: 9.616424465
- λ定义外循环:9.616424465
- Normal function definition and call_user_func(): 13.72040915
- 正常函数定义和call_user_func(): 13.72040915
- Lambda definition outside loop and call_user_func(): 19.98814855
- Lambda定义外部循环和call_user_func(): 19.98814855
Normal function definition and call
普通函数定义和调用
$start_time = microtime(true);
function run_this() {
// Empty function
}
for($i = 0; $i < 5000000; $i++) {
run_this();
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 3.1148610115051
- 3.1148610115051
- 3.0175619125366
- 3.0175619125366
- 3.5064949989319
- 3.5064949989319
- 3.3637712001801
- 3.3637712001801
- 3.0420050621033
- 3.0420050621033
Average: 3.208938837
平均:3.208938837
Lambda definition and call within loop
Lambda定义并在循环中调用
$start_time = microtime(true);
for($i = 0; $i < 5000000; $i++) {
$run_this = function () {
// Empty function
};
$run_this();
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 9.857855797
- 9.857855797
- 10.07680297
- 10.07680297
- 10.35639596
- 10.35639596
- 10.51450491
- 10.51450491
- 10.81063294
- 10.81063294
Average: 10.32323852
平均:10.32323852
Lambda definition outside loop
λ定义外循环
$start_time = microtime(true);
$run_this = function () {
// Empty function
};
for($i = 0; $i < 5000000; $i++) {
$run_this();
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 9.098902941
- 9.098902941
- 9.580584049
- 9.580584049
- 9.370799065
- 9.370799065
- 9.90805316
- 9.90805316
- 10.12378311
- 10.12378311
Average: 9.616424465
平均:9.616424465
Normal function definition and call_user_func()
普通函数定义和call_user_func()
$start_time = microtime(true);
function run_this () {
// Empty function
};
for($i = 0; $i < 5000000; $i++) {
call_user_func('run_this');
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 12.80056691
- 12.80056691
- 13.47905684
- 13.47905684
- 14.51880193
- 14.51880193
- 13.79459596
- 13.79459596
- 14.00902414
- 14.00902414
Average: 13.72040915
平均:13.72040915
Lambda definition and outside loop and call_user_func()
Lambda定义、外部循环和call_user_func()
$start_time = microtime(true);
$run_this = function () {
// Empty function
};
for($i = 0; $i < 5000000; $i++) {
call_user_func($run_this);
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 18.99004483
- 18.99004483
- 20.08601403
- 20.08601403
- 20.52800584
- 20.52800584
- 20.16949892
- 20.16949892
- 20.16717911
- 20.16717911
Average: 19.98814855
平均:19.98814855
Normal function definition with array_map()
使用array_map()的法线函数定义
$arr = array_pad([], 5000, 0);
$start_time = microtime(true);
function run_this () {
// Empty function
};
for($i = 0; $i < 1000; $i++) {
array_map('run_this', $arr);
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 3.727953911
- 3.727953911
- 4.213405848
- 4.213405848
- 4.068621874
- 4.068621874
- 4.180392027
- 4.180392027
- 3.815594912
- 3.815594912
Average: 4.001193714
平均:4.001193714
Lambda definition outside loop with array_map()
带array_map()的Lambda定义外循环
$arr = array_pad([], 5000, 0);
$start_time = microtime(true);
$run_this = function () {
// Empty function
};
for($i = 0; $i < 1000; $i++) {
array_map($run_this, $arr);
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 9.418456793
- 9.418456793
- 10.07496095
- 10.07496095
- 10.1241622
- 10.1241622
- 10.74794412
- 10.74794412
- 10.19295692
- 10.19295692
Average: 10.1116962
平均:10.1116962
#1
3
I executed a function several different ways and recorded the run times. It looks like using a string reference to a regular function is much more efficient.
我以几种不同的方式执行了一个函数并记录了运行时间。看起来对常规函数使用字符串引用要高效得多。
Summary
总结
- Normal function definition with array_map(): 4.001193714
- 使用array_map()的法线函数定义:4.001193714
- Lambda definition outside loop with array_map(): 10.1116962
- 带array_map()的Lambda定义外循环:10.1116962
- Normal function definition and call: 3.208938837
- 普通函数的定义和调用:3.208938837
- Lambda definition and call within loop: 10.32323852
- λ定义和调用内循环:10.32323852
- Lambda definition outside loop: 9.616424465
- λ定义外循环:9.616424465
- Normal function definition and call_user_func(): 13.72040915
- 正常函数定义和call_user_func(): 13.72040915
- Lambda definition outside loop and call_user_func(): 19.98814855
- Lambda定义外部循环和call_user_func(): 19.98814855
Normal function definition and call
普通函数定义和调用
$start_time = microtime(true);
function run_this() {
// Empty function
}
for($i = 0; $i < 5000000; $i++) {
run_this();
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 3.1148610115051
- 3.1148610115051
- 3.0175619125366
- 3.0175619125366
- 3.5064949989319
- 3.5064949989319
- 3.3637712001801
- 3.3637712001801
- 3.0420050621033
- 3.0420050621033
Average: 3.208938837
平均:3.208938837
Lambda definition and call within loop
Lambda定义并在循环中调用
$start_time = microtime(true);
for($i = 0; $i < 5000000; $i++) {
$run_this = function () {
// Empty function
};
$run_this();
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 9.857855797
- 9.857855797
- 10.07680297
- 10.07680297
- 10.35639596
- 10.35639596
- 10.51450491
- 10.51450491
- 10.81063294
- 10.81063294
Average: 10.32323852
平均:10.32323852
Lambda definition outside loop
λ定义外循环
$start_time = microtime(true);
$run_this = function () {
// Empty function
};
for($i = 0; $i < 5000000; $i++) {
$run_this();
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 9.098902941
- 9.098902941
- 9.580584049
- 9.580584049
- 9.370799065
- 9.370799065
- 9.90805316
- 9.90805316
- 10.12378311
- 10.12378311
Average: 9.616424465
平均:9.616424465
Normal function definition and call_user_func()
普通函数定义和call_user_func()
$start_time = microtime(true);
function run_this () {
// Empty function
};
for($i = 0; $i < 5000000; $i++) {
call_user_func('run_this');
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 12.80056691
- 12.80056691
- 13.47905684
- 13.47905684
- 14.51880193
- 14.51880193
- 13.79459596
- 13.79459596
- 14.00902414
- 14.00902414
Average: 13.72040915
平均:13.72040915
Lambda definition and outside loop and call_user_func()
Lambda定义、外部循环和call_user_func()
$start_time = microtime(true);
$run_this = function () {
// Empty function
};
for($i = 0; $i < 5000000; $i++) {
call_user_func($run_this);
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 18.99004483
- 18.99004483
- 20.08601403
- 20.08601403
- 20.52800584
- 20.52800584
- 20.16949892
- 20.16949892
- 20.16717911
- 20.16717911
Average: 19.98814855
平均:19.98814855
Normal function definition with array_map()
使用array_map()的法线函数定义
$arr = array_pad([], 5000, 0);
$start_time = microtime(true);
function run_this () {
// Empty function
};
for($i = 0; $i < 1000; $i++) {
array_map('run_this', $arr);
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 3.727953911
- 3.727953911
- 4.213405848
- 4.213405848
- 4.068621874
- 4.068621874
- 4.180392027
- 4.180392027
- 3.815594912
- 3.815594912
Average: 4.001193714
平均:4.001193714
Lambda definition outside loop with array_map()
带array_map()的Lambda定义外循环
$arr = array_pad([], 5000, 0);
$start_time = microtime(true);
$run_this = function () {
// Empty function
};
for($i = 0; $i < 1000; $i++) {
array_map($run_this, $arr);
}
print "Execution time: " . (microtime(true) - $start_time) . "\n";
- 9.418456793
- 9.418456793
- 10.07496095
- 10.07496095
- 10.1241622
- 10.1241622
- 10.74794412
- 10.74794412
- 10.19295692
- 10.19295692
Average: 10.1116962
平均:10.1116962