惰性评估的优点是什么? [英] What are the advantages of Lazy Evaluation?

查看:134
本文介绍了惰性评估的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与渴望评估相比,懒惰评估有什么优势?

What advantages are there to Lazy Evaluation as opposed to Eager Evaluation?

有哪些性能开销?懒惰评估会变慢还是变快?为什么(或取决于实现?)?

What performance overhead is there? Is Lazy Evaluation going to be slower or faster? Why(or does it depend on implementation?)?

在大多数实现中,惰性评估实际上如何工作?在我看来,由于变量必须存储操作和数字,因此这似乎会慢得多并且占用大量内存.那么在Haskell这样的语言中该如何工作(请注意,我实际上并不知道该语言)?如何实现和完成惰性,以使其不会大大减慢/占用更多空间?

How does lazy evaluation actually work in most implementations? To me it would seem like it would be much slower and memory intensive because variables must stored operations as well as numbers. so how does that work in a language like Haskell(note, I don't actually know that language)? How is the lazyness implemented and done so that it is not tremendously slower/consume more space?

推荐答案

这是对语法树的评估.如果您懒惰地评估语法树(即当需要它表示的值时),则必须将其完整地进行整个计算的前面步骤.这是惰性评估的开销.但是,有两个优点. 1)如果从不使用结果,则不会不必要地撤消该树,并且2)您可以以某种递归形式表达和使用无限语法树,因为您只会将其评估到所需的深度,而不是评估(急切地)将其全部删除,这是不可能的.

This refers to the evaluation of a syntax tree. If you evaluate a syntax tree lazily (i.e. when the value it represents is needed), you must carry it through the preceeding steps of your computation in its entirety. This is the overhead of lazy evaluation. However, there are two advantages. 1) you will not evaulate the tree unnecessarily if the result is never used, and 2) you can express and use an infinite syntax tree in some recursive form, because you will only ever evaluate it to the depth you need, as opposed to evaluating (eagerly) in its entirety, which would be impossible.

我不了解haskel,但据我所知,像python或ML这样的编程语言都非常重视评估.例如,要在ML中模拟延迟评估,必须创建一个不带参数但返回结果的伪函数.然后,该函数就是您的语法树,您可以随时通过使用空的参数列表对其进行懒惰地求值.

I don't know haskel, but as far as I know programming languages like python or ML evaluate eagerly. For example to simulate lazy evaluation in ML, you must create a dummy function that takes no parameters but returns the result. This function is then your syntax tree that you can lazily evaluate at any time by invoking it with an empty argument list.

这篇关于惰性评估的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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