Skip to content

Instantly share code, notes, and snippets.

@hatyuki
Created February 17, 2015 01:44
Show Gist options
  • Save hatyuki/5b487e1764caf628736f to your computer and use it in GitHub Desktop.
Save hatyuki/5b487e1764caf628736f to your computer and use it in GitHub Desktop.
require 'benchmark'
N = 1000000
fixnum = 1234567
float = 1.23456
string = 'abcdefg'
symbol = :symbol!
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 'Float#to_s' do
(1..N).each { float.to_s }
end
x.report '"#{Float}"' do
(1..N).each { "#{float}" }
end
x.report 'Symbol#to_s' do
(1..N).each { symbol.to_s }
end
x.report '"#{Symbol}"' do
(1..N).each { "#{symbol}" }
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.228480)
"#{Fixnum}" 0.320000 0.000000 0.320000 ( 0.322111)
Float#to_s 0.590000 0.000000 0.590000 ( 0.585839)
"#{Float}" 0.730000 0.000000 0.730000 ( 0.724339)
Symbol#to_s 0.140000 0.000000 0.140000 ( 0.141902)
"#{Symbol}" 0.250000 0.000000 0.250000 ( 0.250409)
String#to_s 0.070000 0.000000 0.070000 ( 0.068322)
"#{String}" 0.130000 0.000000 0.130000 ( 0.136455)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment