在PHP中,使用lambda表达式比使用字符串(或数组)更有效吗?

时间:2021-05-04 04:18:02

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

总结

  1. Normal function definition with array_map():                4.001193714
  2. 使用array_map()的法线函数定义:4.001193714
  3. Lambda definition outside loop with array_map():      10.1116962
  4. 带array_map()的Lambda定义外循环:10.1116962
  5. Normal function definition and call:                               3.208938837
  6. 普通函数的定义和调用:3.208938837
  7. Lambda definition and call within loop:                       10.32323852
  8. λ定义和调用内循环:10.32323852
  9. Lambda definition outside loop:                                    9.616424465
  10. λ定义外循环:9.616424465
  11. Normal function definition and call_user_func():         13.72040915
  12. 正常函数定义和call_user_func(): 13.72040915
  13. Lambda definition outside loop and call_user_func(): 19.98814855
  14. 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";
  1. 3.1148610115051
  2. 3.1148610115051
  3. 3.0175619125366
  4. 3.0175619125366
  5. 3.5064949989319
  6. 3.5064949989319
  7. 3.3637712001801
  8. 3.3637712001801
  9. 3.0420050621033
  10. 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";
  1. 9.857855797
  2. 9.857855797
  3. 10.07680297
  4. 10.07680297
  5. 10.35639596
  6. 10.35639596
  7. 10.51450491
  8. 10.51450491
  9. 10.81063294
  10. 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";
  1. 9.098902941
  2. 9.098902941
  3. 9.580584049
  4. 9.580584049
  5. 9.370799065
  6. 9.370799065
  7. 9.90805316
  8. 9.90805316
  9. 10.12378311
  10. 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";
  1. 12.80056691
  2. 12.80056691
  3. 13.47905684
  4. 13.47905684
  5. 14.51880193
  6. 14.51880193
  7. 13.79459596
  8. 13.79459596
  9. 14.00902414
  10. 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";
  1. 18.99004483
  2. 18.99004483
  3. 20.08601403
  4. 20.08601403
  5. 20.52800584
  6. 20.52800584
  7. 20.16949892
  8. 20.16949892
  9. 20.16717911
  10. 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";
  1. 3.727953911
  2. 3.727953911
  3. 4.213405848
  4. 4.213405848
  5. 4.068621874
  6. 4.068621874
  7. 4.180392027
  8. 4.180392027
  9. 3.815594912
  10. 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";
  1. 9.418456793
  2. 9.418456793
  3. 10.07496095
  4. 10.07496095
  5. 10.1241622
  6. 10.1241622
  7. 10.74794412
  8. 10.74794412
  9. 10.19295692
  10. 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

总结

  1. Normal function definition with array_map():                4.001193714
  2. 使用array_map()的法线函数定义:4.001193714
  3. Lambda definition outside loop with array_map():      10.1116962
  4. 带array_map()的Lambda定义外循环:10.1116962
  5. Normal function definition and call:                               3.208938837
  6. 普通函数的定义和调用:3.208938837
  7. Lambda definition and call within loop:                       10.32323852
  8. λ定义和调用内循环:10.32323852
  9. Lambda definition outside loop:                                    9.616424465
  10. λ定义外循环:9.616424465
  11. Normal function definition and call_user_func():         13.72040915
  12. 正常函数定义和call_user_func(): 13.72040915
  13. Lambda definition outside loop and call_user_func(): 19.98814855
  14. 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";
  1. 3.1148610115051
  2. 3.1148610115051
  3. 3.0175619125366
  4. 3.0175619125366
  5. 3.5064949989319
  6. 3.5064949989319
  7. 3.3637712001801
  8. 3.3637712001801
  9. 3.0420050621033
  10. 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";
  1. 9.857855797
  2. 9.857855797
  3. 10.07680297
  4. 10.07680297
  5. 10.35639596
  6. 10.35639596
  7. 10.51450491
  8. 10.51450491
  9. 10.81063294
  10. 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";
  1. 9.098902941
  2. 9.098902941
  3. 9.580584049
  4. 9.580584049
  5. 9.370799065
  6. 9.370799065
  7. 9.90805316
  8. 9.90805316
  9. 10.12378311
  10. 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";
  1. 12.80056691
  2. 12.80056691
  3. 13.47905684
  4. 13.47905684
  5. 14.51880193
  6. 14.51880193
  7. 13.79459596
  8. 13.79459596
  9. 14.00902414
  10. 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";
  1. 18.99004483
  2. 18.99004483
  3. 20.08601403
  4. 20.08601403
  5. 20.52800584
  6. 20.52800584
  7. 20.16949892
  8. 20.16949892
  9. 20.16717911
  10. 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";
  1. 3.727953911
  2. 3.727953911
  3. 4.213405848
  4. 4.213405848
  5. 4.068621874
  6. 4.068621874
  7. 4.180392027
  8. 4.180392027
  9. 3.815594912
  10. 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";
  1. 9.418456793
  2. 9.418456793
  3. 10.07496095
  4. 10.07496095
  5. 10.1241622
  6. 10.1241622
  7. 10.74794412
  8. 10.74794412
  9. 10.19295692
  10. 10.19295692

Average: 10.1116962

平均:10.1116962