为什么PHP发生器比foreach慢? [英] Why php generator is slower than foreach?

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

问题描述

根据文档的意见: http://php.net/manual/en/language.generators .overview.php

我们可以看到,由于生成器有巨大的内存使用改善(这是显而易见的),但也有2-3倍的执行速度 - 并非如此对我来说是显而易见的。

我们在牺牲时间的情况下获得了内存使用的改进 - 这是不好的。

所以,为什么php生成器比foreach?



感谢提示。

在计算机科学中,当进行优化时,很多时候你都不得不在执行速度和内存使用之间进行选择,比如预先计算和存储,或者只是在你需要的时候进行计算。


生成器允许您编写使用foreach迭代
a数据集的代码,而无需在内存中构建数组,而whi可能
导致你超出内存限制,或需要相当数量的
的处理时间来生成

本手册可能指的是您不能遍历所有使用生成器生成的结果的情况。速度优势将来自您不需要通过生成不需要的项目来浪费处理时间和内存的事实。另外,生成器并不是用来替换数组的。他们的目的是为了在实现Iterator对象时减少样板代码。

生成器在与数组进行比较时总是比较慢,因为生成器必须生成每个值你调用 next()来节省内存。

编辑

我有点好奇,在 xrange 之间进行脏比较(使用生成器实现,如 PHP手册页面)和内置的范围函数。
$ b

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

范围(1,10000000,1) :

 时间:5.2702 
内存(字节):1495269376

xrange(1,10000000,1):

 时间:1.9010 
内存(字节):262144

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


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.

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

Thanks for tips.

解决方案

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.

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.

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

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.

Edit

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.

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

range(1, 10000000, 1):

time: 5.2702
memory (byte): 1495269376

xrange(1, 10000000, 1):

time: 1.9010
memory (byte): 262144

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发生器比foreach慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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