Guest User

Untitled

a guest
Apr 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. # Benchmarks for various array operations.
  4. #
  5. # user system total real
  6. # Array.new 0.570000 0.000000 0.570000 ( 0.572061)
  7. # [] 0.380000 0.000000 0.380000 ( 0.383014)
  8. # dup 0.560000 0.000000 0.560000 ( 0.554559)
  9. # replace 0.410000 0.000000 0.410000 ( 0.411269)
  10. # pack * 1.670000 0.000000 1.670000 ( 1.670611)
  11. # pack H40 1.740000 0.000000 1.740000 ( 1.752113)
  12. # pack H40*10 1.180000 0.000000 1.180000 ( 1.179426)
  13. #
  14. Benchmark.bm(20) do |x|
  15. n = 1000000
  16. a = [1,2,3]
  17. b = [4,5,6]
  18.  
  19. x.report("Array.new") do
  20. n.times do
  21. Array.new
  22. end
  23. end
  24.  
  25. x.report("[]") do
  26. n.times do
  27. []
  28. end
  29. end
  30.  
  31. x.report("dup") do
  32. n.times do
  33. a.dup
  34. end
  35. end
  36.  
  37. x.report("replace") do
  38. n.times do
  39. a.replace(b)
  40. end
  41. end
  42.  
  43. x.report("pack *") do
  44. sha = "ee8b5c4ddb843673ef659cfebd0c9e4cbe8ca59d"
  45. a = [sha]
  46. n.times { a.pack("H*") }
  47. end
  48.  
  49. x.report("pack H40") do
  50. sha = "ee8b5c4ddb843673ef659cfebd0c9e4cbe8ca59d"
  51. a = [sha]
  52. n.times { a.pack("H40") }
  53. end
  54.  
  55. x.report("pack H40*10") do
  56. sha = "ee8b5c4ddb843673ef659cfebd0c9e4cbe8ca59d"
  57. a = Array.new(10, sha)
  58. (n/10).times { a.pack("H40" * 10) }
  59. end
  60. end
  61.  
  62. # An interesting thing to note here is how the time for unshift 'exponentially'
  63. # increases with n. The other methods are more linear in time. Note too that
  64. # shift is faster than pop; ie a FIFO queue using push/shift is fastest.
  65. #
  66. # user system total real
  67. # push 10k 0.010000 0.000000 0.010000 ( 0.002962)
  68. # pop 10k 0.000000 0.000000 0.000000 ( 0.003004)
  69. # unshift 10k 0.040000 0.000000 0.040000 ( 0.041590)
  70. # shift 10k 0.000000 0.000000 0.000000 ( 0.002379)
  71. # user system total real
  72. # push 100k 0.030000 0.000000 0.030000 ( 0.027808)
  73. # pop 100k 0.030000 0.000000 0.030000 ( 0.025829)
  74. # unshift 100k 4.130000 0.020000 4.150000 ( 4.223530)
  75. # shift 100k 0.020000 0.000000 0.020000 ( 0.023153)
  76. # user system total real
  77. # push 1000k 0.280000 0.010000 0.290000 ( 0.278975)
  78. # pop 1000k 0.250000 0.000000 0.250000 ( 0.253572)
  79. # shift 1000k 0.220000 0.000000 0.220000 ( 0.219806)
  80. #
  81. [10, 100, 1000].each do |n|
  82.  
  83. Benchmark.bm(20) do |x|
  84. m = n * 1000
  85. array = []
  86. x.report("push #{n}k") do
  87. m.times { array.push(nil) }
  88. end
  89.  
  90. x.report("pop #{n}k") do
  91. m.times { array.pop }
  92. end
  93.  
  94. x.report("unshift #{n}k") do
  95. m.times { array.unshift(nil) }
  96. end unless n > 100
  97.  
  98. x.report("shift #{n}k") do
  99. m.times { array.shift }
  100. end
  101. end
  102. end
Add Comment
Please, Sign In to add comment