I executed the function in several different ways and recorded the runtime. Using a string reference to a regular function seems to be much more efficient.
Summary
- Defining a normal function using array_map (): 4,001193714
- Determining the appearance of Lambda using array_map (): 10.1116962
- Definition and call of a normal function: 3,208938837
- Lambda definition and call inside the loop: 10.32323852
- Lambda outer loop definition: 9.616424465
- Normal function definition and call_user_func (): 13.72040915
- Defining the appearance of Lambda and 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.0175619125366
- +3.5064949989319
- +3.3637712001801
- +3.0420050621033
Average: 3.208938837
Lambda definition and loop call
$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
- 10.07680297
- 10.35639596
- 10.51450491
- 10.81063294
Average: 10.32323852
Lambda outer loop definition
$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,580584049
- 9,370799065
- 9.90805316
- 10.12378311
Average: 9.616424465
Definition of a normal function and 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
- 13.47905684
- 14.51880193
- 13.79459596
- 14.00902414
Average: 13.72040915
Lambda definition and outer loop and 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
- 20.08601403
- 20.52800584
- 20.16949892
- 20.16717911
Average: 19.98814855
Defining a normal function with 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
- 4,213405848
- 4,068621874
- 4,180392027
- 3,815594912
Average value: 4.001193714
External Lambda definition loop with array_map ()
$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
- 10.07496095
- 10.1241622
- 10.74794412
- 10.19295692
Average: 10.1116962