简体   繁体   English

Scala别名参数的性能如何?

[英]How good is Scala By-name parameters performance?

I know how scala by-name parameters work: https://tpolecat.github.io/2014/06/26/call-by-name.html 我知道Scala别名参数如何工作: https//tpolecat.github.io/2014/06/26/call-by-name.html

I'm using it in a very sensitive piece of code that gets run a lot. 我在非常敏感的一段代码中使用它,该代码经常运行。 My questions is: Is there any performance or memory drawback? 我的问题是:是否存在性能或内存缺陷? For example, I know using lazy val has its drawbacks: What's the (hidden) cost of Scala's lazy val? 例如,我知道使用惰性val有其缺点: Scala的惰性val的(隐藏)成本是多少?

Is there something similar about by-name parameters? 别名参数是否有相似之处? How are they implemented underneath? 它们如何在下面实现?

Please note I will NOT be using it with lazy for caching. 请注意,我不会将其与lazy一起用于缓存。 So I wouldn't have that above mentioned problem. 因此,我不会遇到上述问题。 I just need to know under the hood it's not using lazy itself. 我只需要知道它本身并不是在使用惰性。

Thanks 谢谢

By name parameters are implemented as instances of Function1 . 按名称,参数作为Function1实例实现。 So they also have the same performance characteristics. 因此它们也具有相同的性能特征。

Calling a method with a by name parameter has the overhead of creating an instance of Function1 and using the by name parameter has the overhead of calling the method apply on a Function1 object. 使用by name参数调用方法会产生创建Function1实例的开销,而使用by name参数调用会调用将方法apply Function1对象的开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM