Skip to content

Instantly share code, notes, and snippets.

@hatyuki
Last active August 29, 2015 14:15
Show Gist options
  • Save hatyuki/2821106d84bf2df4a22c to your computer and use it in GitHub Desktop.
Save hatyuki/2821106d84bf2df4a22c to your computer and use it in GitHub Desktop.
require 'benchmark'
N = 1000000
fixnum = 1234567
float = 1.234567
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.230000 0.000000 0.230000 ( 0.232231)
"#{Fixnum}" 0.310000 0.010000 0.320000 ( 0.321770)
Float#to_s 0.620000 0.010000 0.630000 ( 0.629486)
"#{Float}" 0.890000 0.040000 0.930000 ( 0.941235)
Symbol#to_s 0.150000 0.000000 0.150000 ( 0.147023)
"#{Symbol}" 0.220000 0.020000 0.240000 ( 0.234657)
String#to_s 0.070000 0.000000 0.070000 ( 0.070717)
"#{String}" 0.130000 0.000000 0.130000 ( 0.141269)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment