Attempting to profile the difference in runtime between "1+1" and "1x2" using Ruby and the gem ruby-prof.
Install the gem and hacked together some code that appears to work, but does not give me the answer I'm looking for, that being the difference in runtime.
Is this possible, and if so, what code would give me this answer.
This code appears to work, but does not allow me to see a runtime difference.
require 'ruby-prof'
result = RubyProf.profile do
1+1
end
printer = RubyProf::GraphPrinter.new(result)
result = RubyProf.profile do
1x2
end
printer = RubyProf::GraphPrinter.new(result)
Which returns this in IRB
irb(main):001:0> require 'ruby-prof'
=> true
irb(main):002:0>
irb(main):003:0* result = RubyProf.profile do
irb(main):004:1* 1+1
irb(main):005:1> end
=> #<RubyProf::Result:0x11050c8>
irb(main):006:0> printer = RubyProf::GraphPrinter.new(result)
=> #<RubyProf::GraphPrinter:0x1332c18 @result=#<RubyProf::Result:0x11050c8>, @ou
tput=nil, @options={}, @thread_times={6793716=>0.01}>
irb(main):007:0>
irb(main):008:0* result = RubyProf.profile do
irb(main):009:1* 1x2
irb(main):010:1> end
SyntaxError: (irb):9: syntax error, unexpected tIDENTIFIER, expecting keyword_en
d
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):011:0> printer = RubyProf::GraphPrinter.new(result)
=> #<RubyProf::GraphPrinter:0x1124310 @result=#<RubyProf::Result:0x11050c8>, @ou
tput=nil, @options={}, @thread_times={6793716=>0.01}>
irb(main):012:0>
1x2
means nothing in ruby. Use1*2
instead.EDIT: You must run the code more times as it is too fast to measure.
Anyway I think the best way to do it is with Benchmark:
It gives me:
Multiplication is just a bit slower. But the difference is too small to mean anything.