首页 \ 问答 \ 为什么Scala中的这个函数调用没有被优化掉?(Why this function call in Scala is not optimized away?)

为什么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
更新时间:2022-07-14 19:07

最满意答案

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是一个 Web 应用程序框架,是一个相对较新的 Web 应用程序框架,构建在 Ruby 语言之上。它被宣传为现有企业框架的一个替代,而它的目标,简而言之,就是让生活,至少是 Web 开发方面的生活,变得更轻松。 J2EE是一种利用Java 2平台来简化企业解决方案的开发、部署和管理相关的复杂问题的体系结构。J2EE技术的基础就是核心Java平台或Java 2平台的标准版,J2EE体系结构提供中间层集成框架用来满足无需太多费用而又需要高可用性能高可靠性以及可扩展性的应用的需求。 ...
  • 您可能想要利用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 ...
  • 这就是您将通知“整合”到您的回复中的方式 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 ...
  • 意外的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 ...
  • 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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。