PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/test-suite/ruby/ruby_li_std_speed_runme.rb

#
Ruby | 82 lines | 63 code | 11 blank | 8 comment | 8 complexity | 723cb3503ec43dbe23cd2c29f9c63001 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. #!/usr/bin/env ruby
  2. #
  3. # This is a simple speed benchmark suite for std containers,
  4. # to verify their O(n) performance.
  5. # It is not part of the standard tests.
  6. require 'benchmark'
  7. require 'mathn'
  8. require 'ruby_li_std_speed'
  9. include Ruby_li_std_speed
  10. def benchmark(f, phigh, sequences)
  11. puts f
  12. print '%10s' % 'n'
  13. maxlen = sequences.max { |a,b| a.to_s.size <=> b.to_s.size }
  14. maxlen = maxlen.to_s.size - 12
  15. sequences.each { |s| print "%#{maxlen}s" % "#{s.to_s.sub(/.*::/,'')}" }
  16. puts
  17. o_perf = Array.new(sequences.size, 0)
  18. last_t = Array.new(sequences.size, nil)
  19. 1.upto(phigh) do |p|
  20. n = 2**(p-1)
  21. print "%10d" % n
  22. sequences.each_with_index do |s, i|
  23. cont = s.new((0..n).to_a)
  24. Benchmark.benchmark('',0,"%#{maxlen-2}.6r") { |x|
  25. t = x.report { f.call(cont) }
  26. o_perf[i] += last_t[i] ? (t.real / last_t[i]) : t.real
  27. last_t[i] = t.real
  28. }
  29. end
  30. puts
  31. end
  32. print " avg. O(n)"
  33. base = 1.0 / Math.log(2.0)
  34. sequences.each_with_index do |s, i|
  35. o_perf[i] /= phigh
  36. # o_perf[i] = 1 if o_perf[i] < 1
  37. o_perf[i] = Math.log(o_perf[i]) * base
  38. print "%#{maxlen-1}.2f " % o_perf[i]
  39. end
  40. puts
  41. end
  42. def iterate(cont)
  43. it = cont.begin
  44. last = cont.end
  45. while it != last
  46. it.next
  47. end
  48. end
  49. def erase(cont)
  50. it = cont.end
  51. # can't reuse begin since it might get invalidated
  52. while it != cont.begin
  53. it.previous
  54. # set returns None, so need to reobtain end
  55. it = cont.erase(it) || cont.end
  56. end
  57. end
  58. def insert(cont)
  59. size = cont.size
  60. size.upto((size<<1) - 1) { |x| cont.push(x) }
  61. end
  62. if $0 == __FILE__
  63. GC.disable
  64. sequences = [RbVector,RbDeque,RbSet,RbList,
  65. RbFloatVector,RbFloatDeque,RbFloatSet,RbFloatList]
  66. n = 17
  67. for f,phigh in [[method(:iterate),n], [method(:insert),n],
  68. [method(:erase),n-4]]
  69. benchmark(f, phigh, sequences)
  70. end
  71. end