require 'benchmark'
n = 500000
puts "convertions"
Benchmark.bm do |x|
x.report("to_i") { n.times do ; "123456789".to_i; end }
x.report("to_s") { n.times do ; 1234567890.to_s; end }
x.report("to_sym") { n.times do ; 1234567890.to_sym; end }
end
user system total real
to_i 0.310000 0.000000 0.310000 ( 0.315844)
to_s 0.490000 0.000000 0.490000 ( 0.490607)
to_sym 0.170000 0.000000 0.170000 ( 0.171511)
Benchmark.bm(30) do |x|
x.report("string.to_i == number") { n.times do ; "123456789".to_i == 1234567890; end }
x.report("number.to_s == string") { n.times do ; 1234567890.to_s == '1234567890'; end }
x.report("string.to_sym == string.to_sym") { n.times do ; '1234567890'.to_sym == '1234567890'.to_sym; end }
x.report("num.to_sym == string.to_sym") { n.times do ; 1234567890.to_sym == '1234567890'.to_sym; end }
x.report("number.sym == number.sym") { n.times do ; 1234567890.to_sym == 1234567890.to_sym; end }
end
user system total real
string.to_i == number 0.400000 0.000000 0.400000 ( 0.401863)
number.to_s == string 0.740000 0.000000 0.740000 ( 0.744603)
string.to_sym == string.to_sym 0.560000 0.000000 0.560000 ( 0.574106)
num.to_sym == string.to_sym 0.460000 0.000000 0.460000 ( 0.459292)
number.sym == number.sym 0.400000 0.000000 0.400000 ( 0.399014)
© 2025 Gudasoft
Theme by Anders Norén — Up ↑