加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – “-r profile”和“require’profile’”之间有什么区别

发布时间:2020-12-17 02:06:15 所属栏目:百科 来源:网络整理
导读:我的 Ruby版本是1.9.3p448. 我有一个简单的程序: count = 0words = File.open('/usr/share/dict/words')while word = words.gets word.chomp! count += 1 if word.length == 12endputs "#{count} twelve-character words" 当我用ruby -r profile test.rb评
我的 Ruby版本是1.9.3p448.

我有一个简单的程序:

count = 0
words = File.open('/usr/share/dict/words')

while word = words.gets
  word.chomp!
  count += 1 if word.length == 12
end

puts "#{count} twelve-character words"

当我用ruby -r profile test.rb评估它时,输出是:

7226 twelve-character words
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
  9.63     0.44      0.44   119095     0.00     0.00  String#chomp!
  9.19     0.86      0.42   119095     0.00     0.00  Fixnum#==
  6.35     1.15      0.29   119095     0.00     0.00  String#length
  5.91     1.42      0.27   119096     0.00     0.00  IO#gets
  0.66     1.45      0.03     7226     0.00     0.00  Fixnum#+
  0.00     1.45      0.00        2     0.00     0.00  IO#set_encoding
  0.00     1.45      0.00        1     0.00     0.00  IO#open
  0.00     1.45      0.00        1     0.00     0.00  File#initialize
  0.00     1.45      0.00        1     0.00     0.00  Fixnum#to_s
  0.00     1.45      0.00        2     0.00     0.00  IO#write
  0.00     1.45      0.00        1     0.00     0.00  IO#puts
  0.00     1.45      0.00        1     0.00     0.00  Kernel.puts
  0.00     4.57      0.00        1     0.00  4570.00  #toplevel

但是,当我在程序开头添加require’profile’,然后使用ruby test.rb进行评估时,输出为:

7226 twelve-character words
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 25.61     0.63      0.63   119095     0.01     0.01  String#chomp!
 16.67     1.04      0.41   119096     0.00     0.00  IO#gets
  0.00     1.04      0.00        1     0.00     0.00  Fixnum#to_s
  0.00     1.04      0.00        1     0.00     0.00  IO#open
  0.00     1.04      0.00        1     0.00     0.00  File#initialize
  0.00     1.04      0.00        2     0.00     0.00  IO#write
  0.00     1.04      0.00        1     0.00     0.00  IO#puts
  0.00     1.04      0.00        1     0.00     0.00  Kernel.puts
  0.00     2.46      0.00        1     0.00  2460.00  #toplevel

似乎第二种方法丢失了一些方法,如Fixnum#==,Fixnum#,String #length和IO#set_encoding.

为什么它与第一个没有相同的结果?

解决方法

根据 profile documentation,应该没有区别.我为此创建了一个 bug report.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读