In PHP, is it more efficient to use a lambda expression for the called than a string (or array)?

In PHP, some functions take a “called” as an argument, which means that you can specify a function that will execute at some point down the line. For example, array_map .

PHP allows you to specify the called in several ways , for example:

 // 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('Ym-d'); $dates = array_map(array($factory, 'newDate'), $arrayOfDateStrings); // as a lambda expression / closure: $dates = array_map( function ($dateString) { return DateTime::createFromFormat('Ym-d', $dateString); }, $arrayOfDateStrings ); 

Now I believe that the last form is evaluated by the PHP engine once at compile time, and the other can be evaluated at runtime, probably for each call, that is, using closure will be much more efficient for a large number of calls, is this an assumption?

+6
source share
1 answer

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

+3
source

Source: https://habr.com/ru/post/984176/


All Articles