为什么Scala中的这个函数调用没有被优化掉?(Why this function call in Scala is not optimized away?)
我正在使用Scala 2.10.3运行此程序:
object Test { def main(args: Array[String]) { def factorial(x: BigInt): BigInt = if (x == 0) 1 else x * factorial(x - 1) val N = 1000 val t = new Array[Long](N) var r: BigInt = 0 for (i <- 0 until N) { val t0 = System.nanoTime() r = r + factorial(300) t(i) = System.nanoTime()-t0 } val ts = t.sortWith((x, y) => x < y) for (i <- 0 to 10) print(ts(i) + " ") println("*** " + ts(N/2) + "\n" + r) } }
并且在每次循环迭代期间评估具有常量参数的纯函数
factorial
(基于时序结果的结论)。 第一次调用后,优化器不应该重用函数调用结果吗?我正在使用Scala IDE for Eclipse。 编译器是否有任何优化标志,这可能会产生更高效的代码?
I'm running this program with Scala 2.10.3:
object Test { def main(args: Array[String]) { def factorial(x: BigInt): BigInt = if (x == 0) 1 else x * factorial(x - 1) val N = 1000 val t = new Array[Long](N) var r: BigInt = 0 for (i <- 0 until N) { val t0 = System.nanoTime() r = r + factorial(300) t(i) = System.nanoTime()-t0 } val ts = t.sortWith((x, y) => x < y) for (i <- 0 to 10) print(ts(i) + " ") println("*** " + ts(N/2) + "\n" + r) } }
and call to a pure function
factorial
with constant argument is evaluated during each loop iteration (conclusion based on timing results). Shouldn't the optimizer reuse function call result after the first call?I'm using Scala IDE for Eclipse. Are there any optimization flags for the compiler, which may produce more efficient code?
原文:https://stackoverflow.com/questions/19316276
最满意答案
kEND常量指的是令牌“结束”,就像你结束每个代码块一样。 确保块相当于其他语言中的finally块。
begin 1/0 rescue ZeroDivisionError puts "OH SHI-" ensure # <- THIS THING 1/1 puts "Whew, we're safe" end
这就是kENSURE所指的。
这听起来像是你忘记了在方法调用或方法参数列表的末尾放置结束符。
The kEND constant refers to the token "end", as in what you end every code block with. An ensure block is the equivalent of a finally block in other languages.
begin 1/0 rescue ZeroDivisionError puts "OH SHI-" ensure # <- THIS THING 1/1 puts "Whew, we're safe" end
That's what kENSURE refers to.
It sounds like you forgot to put the closing paren at the end of either a method call or a method parameter list.
相关问答
更多-
Ruby on Rails 是什么?[2022-07-19]
Ruby on Rails是一个 Web 应用程序框架,是一个相对较新的 Web 应用程序框架,构建在 Ruby 语言之上。它被宣传为现有企业框架的一个替代,而它的目标,简而言之,就是让生活,至少是 Web 开发方面的生活,变得更轻松。 J2EE是一种利用Java 2平台来简化企业解决方案的开发、部署和管理相关的复杂问题的体系结构。J2EE技术的基础就是核心Java平台或Java 2平台的标准版,J2EE体系结构提供中间层集成框架用来满足无需太多费用而又需要高可用性能高可靠性以及可扩展性的应用的需求。 ... -
如何在Ruby on Rails中分别渲染XML错误消息?(How to render XML error messages separately in Ruby on Rails?)[2022-05-23]
您可能想要利用render方法可以呈现自定义XML和HTTP状态的事实,如下所示: # Renders 'Not found ' render :xml => { :error => 'Not found' }, :status => 404 You probably want to take advantage of the fact that the render method can render custom XML and an HTTP status, like s ... -
Ruby on Rails,:注意(Ruby on Rails, :notice)[2023-06-10]
这就是您将通知“整合”到您的回复中的方式 def create p_attr=params[:upload] p_attr[:arraydb] = params[:upload][:upload].first if params[:upload][:upload].class == Array @upload = Upload.new(p_attr) respond_to do |format| if @upload.save @upload.update_attrib ... -
generate_full_message的文档可能有用: 任何区域设置的默认full_message格式为“ {{attribute}} {{message}} ”。 可以通过将其存储为密钥的转换来指定特定于语言环境的默认full_message格式:“ activerecord.errors.full_messages.format ”。 此外,可以通过存储以下内容的转换来指定验证特定的错误消息格式:“ activerecord.errors.full_messages.[message_key] ”。 ...
-
显示错误的最常见方法是使用helper error_messages : <%= f.error_messages %> 据我所知,它为存储在@my_object.errors哈希中的错误消息添加了一些html标记。 错误消息是有序散列的,它们是为了在模型中指定验证(至少它在我的应用程序中如此工作)。 您可以在此处阅读有关Errors类的更多信息。 由于error_messages只是一个帮助器,因此您可以编写自己的帮助程序来显示错误消息。 Most common way to display error ...
-
关于Ruby错误'kEnd'?(About Ruby error 'kEnd'?)[2023-08-31]
意外的kEND是在不应该找到的地方找到end关键字的地方。 通常你已经关闭了太多的代码块,或者你有一些其他的语法问题。 如果你粘贴一个有问题的(完整)文件,我们可以指出错误... an unexpected kEND is where the end keyword was found somewhere it shouldn't be. Generally you've closed too many code blocks, or you've got some other syntax problem ... -
在Ruby on Rails中,错误消息中的kENSURE和kEND是什么?(in Ruby on Rails, what is the kENSURE and kEND in error messages?)[2023-07-11]
kEND常量指的是令牌“结束”,就像你结束每个代码块一样。 确保块相当于其他语言中的finally块。 begin 1/0 rescue ZeroDivisionError puts "OH SHI-" ensure # <- THIS THING 1/1 puts "Whew, we're safe" end 这就是kENSURE所指的。 这听起来像是你忘记了在方法调用或方法参数列表的末尾放置结束符。 The kEND constant refers to the token "end" ... -
该版本的exec-js需要Ruby 1.9或更高版本,因为它使用新的哈希语法。 你必须降级exec-js,或升级你的Ruby。 由于Ruby 1.8.7 在一年前达到了寿命终结 ,因此正确的答案是升级Ruby。 That version of exec-js requires Ruby 1.9 or greater, as it uses the new hash syntax. You'll have to downgrade exec-js, or upgrade your Ruby. Since Ru ...
-
尝试从表单行中删除= : <% form_tag(search_path, :method => "get") do %> Try to remove = from form line: <% form_tag(search_path, :method => "get") do %>
-
正如@Prakash Murty所说,你似乎在你的Gemfile中定义了ruby版本。 此行用于Heroku以指定您的ruby版本。 如果要使用它,则需要使用bundler 1.2.0版。 更多信息: https : //devcenter.heroku.com/articles/ruby-versions 但是,如果您不使用heroku来托管您的应用程序,我建议您删除这行Gemfile。 我希望这有帮助。 As @Prakash Murty said you seem to define the ruby ...