Guest User

Untitled

a guest
Apr 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. # Benchmarks for various symbol operations.
  4. #
  5. # user system total real
  6. # :a 0.130000 0.000000 0.130000 ( 0.133211)
  7. # :a to_sym 0.210000 0.000000 0.210000 ( 0.212942)
  8. # :a to_s 0.540000 0.000000 0.540000 ( 0.536892)
  9. # :a..z to_s 0.530000 0.000000 0.530000 ( 0.540183)
  10. # {}[:sym] 0.320000 0.000000 0.320000 ( 0.315051)
  11. #
  12. Benchmark.bm(20) do |x|
  13.  
  14. n = 1000000
  15. a = "a"
  16. z = ('a'..'z').to_a.join
  17.  
  18. a = a.to_sym
  19. x.report ":a" do
  20. n.times { a }
  21. end
  22.  
  23. x.report ":a to_sym" do
  24. n.times { a.to_sym }
  25. end
  26.  
  27. x.report ":a to_s" do
  28. n.times { a.to_s }
  29. end
  30.  
  31. z = z.to_sym
  32. x.report ":a..z to_s" do
  33. n.times { z.to_s }
  34. end
  35.  
  36. hash = {a => 1, z => 2}
  37. x.report "{}[:sym]" do
  38. n.times { hash[z] }
  39. end
  40. end
Add Comment
Please, Sign In to add comment