I wrote some Ruby code to solve a little puzzle (
#14 Longest Collatz Sequence - Project Euler) that functions
correctly. It solves for the answer and gets it right, it’s just a
little
slower than I would like. So I thought I would bring out the old
profiler
and see what I could do to speed things up. Well… on the exact same
code
file, no changes besides ‘require “profile”’ it now gives me this error:
c:/ruby/lib/ruby/1.8/profiler.rb:13:in hash': can't convert Hash into Integer (TypeError) from c:/ruby/lib/ruby/1.8/profiler.rb:13:in
[]’
from c:/ruby/lib/ruby/1.8/profiler.rb:13
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:55:in initialize' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:55:in
initialize’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:122:in new' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:122:in
find’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:120:in each' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:120:in
find’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:137
It seems like it is related to the my initialize method, but it is
simple
and correct:
def initialize(num)
@latest = num
@sequence = [@latest]
end
So I decided to abandon the standard profiler and try RubyProf instead.
Using #require ‘unprof’, it also keels over:
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:33: warning:
ruby-prof: An error occured when leaving the method Kernel#require.
Perhaps an exception occured in the code being profiled?
And using require ‘ruby-prof’ and putting RubyProf.start and
RubyProf.stoparound the code I would like to profile results in:
C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:122:in initialize': undefined method
%’ for
#Hash:0x2b8b9cc (NoMethodError)
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:122:in new' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:122:in
find’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:120:in each' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:120:in
find’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:137
I truly have no idea what is going on here. I’ve checked time and time
again, and this code works correctly without the profilers. I’ve ran it
with ruby -c -w, and it results in no warnings and the code is valid.
Is
the profiler trying to do something unusual here that I should be aware
of?
Thanks for any help, it is much appreciated.
–Tyler P.