Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark'
- # Benchmarks for various symbol operations.
- #
- # user system total real
- # :a 0.130000 0.000000 0.130000 ( 0.133211)
- # :a to_sym 0.210000 0.000000 0.210000 ( 0.212942)
- # :a to_s 0.540000 0.000000 0.540000 ( 0.536892)
- # :a..z to_s 0.530000 0.000000 0.530000 ( 0.540183)
- # {}[:sym] 0.320000 0.000000 0.320000 ( 0.315051)
- #
- Benchmark.bm(20) do |x|
- n = 1000000
- a = "a"
- z = ('a'..'z').to_a.join
- a = a.to_sym
- x.report ":a" do
- n.times { a }
- end
- x.report ":a to_sym" do
- n.times { a.to_sym }
- end
- x.report ":a to_s" do
- n.times { a.to_s }
- end
- z = z.to_sym
- x.report ":a..z to_s" do
- n.times { z.to_s }
- end
- hash = {a => 1, z => 2}
- x.report "{}[:sym]" do
- n.times { hash[z] }
- end
- end
Add Comment
Please, Sign In to add comment