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

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

问题描述

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


  • 实际上是否会影响性能或者是城市传奇吗?
  • b $ b
  • 什么是性能影响(希望有人已经完成了基准测试)?

  • 是什么导致了这种影响(如果存在)?

  • 是固定成本还是每次执行?



任何资源你都会非常感激:)



预先感谢

解决方案

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


  1. 函数的名称( array_map('test',$ myArray);
  2. 包含闭包( array_map($ test,$ myArray); )的变量

  3. 闭包( array_map(function {}(),$ myArray);

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



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

 函数:0.693s 
变量:0.703s
关闭:0.694s

数组数量为10.000.000的项目,结果如下:

pre $ 函数:8.913s
变量:8.169s
Closure:8.117s

所以在任何情况下我们都不会有太多开销, p>

另请参阅 http://fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures
涉及到相同的结论。在这个评论中,你也看到 create_function()明显变慢。


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

解决方案

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.

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

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