为什么PHP Generator比数组要慢? [英] Why php generator is slower than an array?

查看:74
本文介绍了为什么PHP Generator比数组要慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档中的注释: http://php.net/manual /en/language.generators.overview.php
我们可以看到,由于生成器的存在,内存使用率有了巨大的提高(这是显而易见的),但是执行速度却慢了2-3倍-这对我而言并不是那么明显.

According to comments from documentation: http://php.net/manual/en/language.generators.overview.php
We can see that thanks to generators there is huge memory usage improvement (which is obvious), but there is also 2-3 times slower execution - and that is not so obvious to me.

我们以牺牲时间为代价来提高内存使用率-这不是很好.
那么,为什么php generator比数组要慢?

We gain memory usage improvement at the expense of time - which is not fine.
So, why is php generator slower than an array?

感谢提示.

推荐答案

在计算机科学中,很多时候进行优化时,您将不得不在执行速度和内存使用之间进行选择,即预先计算并存储它,或者只是做需要时进行计算.

In computer science, when doing optimizations a lot of the times you will have to choose between speed of execution and memory usage i.e precalculating something and storing it or just doing calculations when you need them.

生成器允许您编写使用foreach进行迭代的代码 一组数据,而无需在内存中构建数组,这可能 导致您超出内存限制,或需要大量内存 处理时间的产生

A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate

该手册可能是指您不会迭代使用生成器生成的所有结果的情况.这样一来,速度优势便来自于这样一个事实,即您不需要通过生成不需要的项目来浪费处理时间和内存.

The manual is probably referring to a situation when you will not iterate through all of the results that you generate with your generator. The speed benefit will then come from the fact that you don't need to waste processing time and memory by generating items that you don't need.

此外,生成器并非旨在替换阵列.它们旨在作为实现Iterator对象时减少样板代码的一种方式.

Also, the generators weren't designed to replace arrays. They were intended as a way to reduce boilerplate code when implementing Iterator objects.

将生成器与数组进行比较时,生成器总是会变慢,因为生成器必须在每次调用next()时生成值以节省内存.

The generators will always be slower when comparing them to arrays, because generator has to generate the values each time you call next() to save memory.

修改

我有点好奇,所以我在xrange(用生成器实现,如

I was a little curious, so I did a quick and dirty comparson between xrange (implemented with generators, as on the PHP manual page) and the built in range function.

我的机器上的结果(用PHP 5.6测试)是:

Results on my machine (tested with PHP 5.6) were:

range(1,10000000,1):

range(1, 10000000, 1):

time: 5.2702
memory (byte): 1495269376

xrange(1,10000000,1):

xrange(1, 10000000, 1):

time: 1.9010
memory (byte): 262144

请注意,我使用的基准"代码是遍历所有结果并进行简单的数学运算.如上所示,该函数调用仅作为我正在测试的值的参考.与往常一样,使用像这样的简单基准测试,YMMV.

Note that the "benchmark" code I was using was iterating through all results and doing simple math operations. The function calls, as displayed above, serve only as a reference for values I was testing with. As always, with simple benchmarks like this, YMMV.

这篇关于为什么PHP Generator比数组要慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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