首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"-r profile“和"require 'profile'”有什么区别?

"-r profile“和"require 'profile'”有什么区别?
EN

Stack Overflow用户
提问于 2014-04-04 19:27:55
回答 1查看 201关注 0票数 4

我的Ruby版本是1.9.3p448。

我有一个简单的程序:

代码语言:javascript
复制
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评估它时,输出是:

代码语言:javascript
复制
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对其求值时,输出是:

代码语言:javascript
复制
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#lengthIO#set_encoding

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

EN

回答 1

Stack Overflow用户

发布于 2014-04-05 00:12:41

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22861559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档