Skip to content

Instantly share code, notes, and snippets.

@hatyuki
Last active August 29, 2015 14:15
Show Gist options
  • Save hatyuki/0fdaf10f177695955782 to your computer and use it in GitHub Desktop.
Save hatyuki/0fdaf10f177695955782 to your computer and use it in GitHub Desktop.
require 'benchmark'
N = 1000000
fixnum = 1234567
string = 'abcdefg'
Benchmark.bm(11) do |x|
x.report 'Fixnum#to_s' do
(1..N).each { fixnum.to_s }
end
x.report '"#{Fixnum}"' do
(1..N).each { "#{fixnum}" }
end
x.report 'String#to_s' do
(1..N).each { string.to_s }
end
x.report '"#{String}"' do
(1..N).each { "#{string}" }
end
end
=begin
user system total real
Fixnum#to_s 0.220000 0.000000 0.220000 ( 0.229589)
"#{Fixnum}" 0.320000 0.000000 0.320000 ( 0.326202)
String#to_s 0.070000 0.000000 0.070000 ( 0.072514)
"#{String}" 0.130000 0.000000 0.130000 ( 0.132092)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment