My Ruby version is 1.9.3p448.
I have a simple program:
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"
When I evaluate it using ruby -r profile test.rb, the output is:
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
9.19 0.86 0.42 119095 0.00 0.00 Fixnum
6.35 1.15 0.29 119095 0.00 0.00 String
5.91 1.42 0.27 119096 0.00 0.00 IO
0.66 1.45 0.03 7226 0.00 0.00 Fixnum
0.00 1.45 0.00 2 0.00 0.00 IO
0.00 1.45 0.00 1 0.00 0.00 IO
0.00 1.45 0.00 1 0.00 0.00 File
0.00 1.45 0.00 1 0.00 0.00 Fixnum
0.00 1.45 0.00 2 0.00 0.00 IO
0.00 1.45 0.00 1 0.00 0.00 IO
0.00 1.45 0.00 1 0.00 0.00 Kernel.puts
0.00 4.57 0.00 1 0.00 4570.00
But, when I add require 'profile'at the beginning of the program, and then evaluate it with ruby test.rb, the output is:
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
16.67 1.04 0.41 119096 0.00 0.00 IO
0.00 1.04 0.00 1 0.00 0.00 Fixnum
0.00 1.04 0.00 1 0.00 0.00 IO
0.00 1.04 0.00 1 0.00 0.00 File
0.00 1.04 0.00 2 0.00 0.00 IO
0.00 1.04 0.00 1 0.00 0.00 IO
0.00 1.04 0.00 1 0.00 0.00 Kernel.puts
0.00 2.46 0.00 1 0.00 2460.00
It seems that the second method has lost some methods, such as Fixnum#==, Fixnum#+, String#lengthand IO#set_encoding.
Why doesn't he have the same result as the first?
source
share