PHP中的匿名函数性能 [英] anonymous function performance in PHP

查看:30
本文介绍了PHP中的匿名函数性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在 php 中使用函数式编程范式,并想知道性能影响是什么.一些谷歌搜索似乎只是说有一些.具体来说,我想知道:

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:

  • 是否真的会对性能产生影响,还是只是一个都市传说?
  • 性能影响是什么(希望有人做过基准测试)?
  • 是什么导致了这种影响(如果存在)?
  • 是固定成本还是每次执行?

你们拥有的任何资源将不胜感激:)

Any resources you guys have would be greatly appreciated :)

提前致谢

推荐答案

我用 array_map() 做了一些测试,调用它:

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

  1. 函数名 (array_map('test', $myArray);)
  2. 一个包含闭包的变量 (array_map($test, $myArray);)
  3. 一个闭包 (array_map(function{}(), $myArray);)

在所有三种情况下,函数都是空的 (function test(){})

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

具有 1.000.000 个项目的数组的结果 ($myArray = range(1,1000000);)

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

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

对于包含 10.000.000 个项目的数组,结果如下:

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.

另见关于的第四条评论http://fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures它得出相同的结论.在该评论中,您还看到 create_function() 明显变慢了.

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.

这篇关于PHP中的匿名函数性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆