anonymous function performance in PHP [closed]

倾然丶 夕夏残阳落幕 提交于 2019-12-01 14:07:09

问题


I'm starting to use functional programming paradigms in php and was wondering what the performance impacts are. Some googling just seems to say that there are some. To be specific, I would like to know:

  • Is there actually a performance impact or is it an urban legend?
  • What is the performance impact (hopefully someone out that has done benchmarks)?
  • What causes this impact (if one exists)?
  • Is it fixed cost, or per execution?

Any resources you guys have would be greatly appreciated :)

Thanks in advance


回答1:


I did some testing with array_map(), calling it with:

  1. The name of a function (array_map('test', $myArray);)
  2. A variable that contains a closure (array_map($test, $myArray);)
  3. A closure (array_map(function{}(), $myArray);)

In all three cases, the function was empty (function test(){})

The results for an array with 1.000.000 items ($myArray = range(1,1000000);)

Function: 0.693s
Variable:0.703s
Closure: 0.694s

For an array of 10.000.000 items, the results are this:

Function: 8.913s
Variable: 8.169s
Closure: 8.117s

So in neither case do we have much overhead, if any.

Also see the 4th comment on http://fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures It comes to the same conclusions. In that comment, you also see that create_function() is significantly slower.



来源:https://stackoverflow.com/questions/8676888/anonymous-function-performance-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!