~ubuntu-branches/ubuntu/oneiric/swig1.3/oneiric

« back to all changes in this revision

Viewing changes to Examples/test-suite/ruby/li_std_speed_runme.rb

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-12-06 10:27:08 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071206102708-t37t62i45n595w0e
Tags: 1.3.33-2ubuntu1
* Merge with Debian; remaining changes:
  - Drop support for pike.
  - Use python2.5 instead of python2.4.
  - Clean Runtime/ as well.
  - Force a few environment variables.
* debian/Rules (clean): Remove Lib/ocaml/swigp4.ml.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#
 
7
# License::   SWIG
 
8
#
 
9
 
 
10
 
 
11
require 'benchmark'
 
12
require 'mathn'
 
13
require 'li_std_speed'
 
14
include Li_std_speed
 
15
 
 
16
 
 
17
def benchmark(f, phigh, sequences)
 
18
  puts f
 
19
  print '%10s' % 'n'
 
20
  maxlen = sequences.max { |a,b| a.to_s.size <=> b.to_s.size }
 
21
  maxlen = maxlen.to_s.size - 12
 
22
  sequences.each { |s| print "%#{maxlen}s" % "#{s.to_s.sub(/.*::/,'')}" }
 
23
  puts
 
24
  o_perf = Array.new(sequences.size, 0)
 
25
  last_t = Array.new(sequences.size, nil)
 
26
  1.upto(phigh) do |p|
 
27
    n = 2**(p-1)
 
28
    print "%10d" % n
 
29
    sequences.each_with_index do |s, i|
 
30
      cont = s.new((0..n).to_a)
 
31
      Benchmark.benchmark('',0,"%#{maxlen-2}.6r") { |x|
 
32
        t = x.report { f.call(cont) }
 
33
        o_perf[i] += last_t[i] ? (t.real / last_t[i]) : t.real
 
34
        last_t[i] = t.real
 
35
      }
 
36
    end
 
37
    puts
 
38
  end
 
39
 
 
40
  print "  avg. O(n)"
 
41
  base = 1.0 / Math.log(2.0)
 
42
  sequences.each_with_index do |s, i|
 
43
    o_perf[i] /= phigh
 
44
    # o_perf[i] = 1 if o_perf[i] < 1
 
45
    o_perf[i]  = Math.log(o_perf[i]) * base
 
46
    print "%#{maxlen-1}.2f " % o_perf[i]
 
47
  end
 
48
  puts
 
49
end
 
50
 
 
51
def iterate(cont)
 
52
   it = cont.begin
 
53
   last = cont.end
 
54
   while it != last 
 
55
     it.next
 
56
   end
 
57
end
 
58
 
 
59
 
 
60
def erase(cont)
 
61
   it = cont.end
 
62
   # can't reuse begin since it might get invalidated
 
63
   while it != cont.begin
 
64
     it.previous
 
65
     # set returns None, so need to reobtain end
 
66
     it = cont.erase(it) || cont.end
 
67
   end
 
68
end
 
69
 
 
70
def insert(cont)
 
71
  size = cont.size
 
72
  size.upto((size<<1) - 1) { |x| cont.push(x) }
 
73
end
 
74
 
 
75
if $0 == __FILE__
 
76
  GC.disable
 
77
  sequences = [RbVector,RbDeque,RbSet,RbList,
 
78
               RbFloatVector,RbFloatDeque,RbFloatSet,RbFloatList]
 
79
  n = 17
 
80
  for f,phigh in [[method(:iterate),n], [method(:insert),n],
 
81
                  [method(:erase),n-4]]
 
82
    benchmark(f, phigh, sequences)
 
83
  end
 
84
end
 
85