amc*_*usl 13 php functional-programming anonymous-function php-5.3
我开始在php中使用函数式编程范例,并且想知道性能影响是什么.一些谷歌搜索似乎只是说有一些.具体来说,我想知道:
你们所拥有的任何资源都将不胜感激:)
提前致谢
Jor*_*rts 18
我用array_map()做了一些测试,用它调用它:
array_map('test', $myArray);)array_map($test, $myArray);)的变量array_map(function{}(), $myArray);)在所有三种情况下,函数都是空的(function test(){})
包含1.000.000项的数组的结果($myArray = range(1,1000000);)
Function: 0.693s
Variable:0.703s
Closure: 0.694s
Run Code Online (Sandbox Code Playgroud)
对于10.000.000项的数组,结果如下:
Function: 8.913s
Variable: 8.169s
Closure: 8.117s
Run Code Online (Sandbox Code Playgroud)
所以在这两种情况下我们都没有太多的开销,如果有的话.
另见http://fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures上的第4条评论
得出相同的结论.在那个评论中,你也看到它create_function()明显变慢了.