Guest User

Untitled

a guest
Aug 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. require 'set'
  2. require 'benchmark'
  3.  
  4. ELEMENTS = (ARGV[0] || 10_000_000).to_i
  5.  
  6. Benchmark.bmbm( 24 ){|test|
  7. test.report('Init Hash'){
  8. @hash = {}
  9. for i in (1 .. ELEMENTS).to_a.shuffle
  10. @hash[i] = i
  11. end
  12. }
  13. test.report('Init Set'){
  14. @set = Set.new( (1 .. ELEMENTS).to_a.shuffle )
  15. }
  16. test.report('Init Set (loop)'){
  17. s = Set.new
  18. for i in (1 .. ELEMENTS).to_a.shuffle
  19. s << i
  20. end
  21. }
  22. test.report('Init Array'){
  23. @array = (1 .. ELEMENTS).to_a.shuffle
  24. }
  25. test.report('Init Array (loop)'){
  26. a = []
  27. for i in (1 .. ELEMENTS).to_a.shuffle
  28. a << i
  29. end
  30. }
  31. test.report('Enum. Hash'){
  32. for (k, v) in @hash
  33. k * v
  34. end
  35. }
  36. test.report('Enum. Hash (keys)'){
  37. for k in @hash.keys
  38. @hash[k]
  39. end
  40. }
  41. test.report('Enum. Hash (rand keys)'){
  42. for k in @hash.keys.shuffle
  43. @hash[k]
  44. end
  45. }
  46. test.report('Enum. Set'){
  47. for v in @set
  48. v * v
  49. end
  50. }
  51. test.report('Enum. Set (rand)'){
  52. for v in @set.to_a.shuffle
  53. v * v
  54. end
  55. }
  56. test.report('Enum. Array'){
  57. for v in @array
  58. v * v
  59. end
  60. }
  61. test.report('Enum. Array (rand)'){
  62. for v in @array.shuffle
  63. v * v
  64. end
  65. }
  66. test.report('Enum. Set -> Array'){
  67. for v in @set.to_a
  68. v * v
  69. end
  70. }
  71. test.report('Find Hash'){
  72. for i in (1 .. ELEMENTS).to_a.shuffle
  73. @hash[i]
  74. end
  75. }
  76. test.report('Find Set'){
  77. for i in (1 .. ELEMENTS).to_a.shuffle
  78. @set.include? i
  79. end
  80. }
  81. test.report('Find Array'){
  82. for i in (1 .. ELEMENTS).to_a.shuffle
  83. @array[i]
  84. end
  85. }
  86. }
Add Comment
Please, Sign In to add comment