Merge branch 'master' of git://iolanguage.com/Io
[io.git] / samples / speed / speed.rb
blobb3e8a03ed8171eb70212c4010ffcb2bf00582e09
1 $oneMillion = 1000000
3 class Tester
4   attr_accessor :t1, :x
6   def foo
7     1
8   end
10   def beginTimer
11     @t1 = Time.now.to_f
12   end
14   def endTimer(s)
15     mps = 1.00 / (Time.now.to_f - @t1)
16     printf "Ruby %s := %0.2f\n", s, mps
17   end
19   def testSlot
20     beginTimer()
21     @x = 1
22     ($oneMillion/8).times {
23       x; x; x; x;
24       x; x; x; x;
25     }
26     endTimer("slotAccesses       ")
27   end
29   def testSetSlot
30     beginTimer
31     @x = 1
32     ($oneMillion/8).times {
33       @x = 1; @x = 1; @x = 1; @x = 1;
34       @x = 1; @x = 1; @x = 1; @x = 1;
35     }
36     endTimer("slotSets           ")
37   end
39   def testBlock
40     beginTimer
41     ($oneMillion/8).times {
42       foo; foo; foo; foo;
43       foo; foo; foo; foo;
44     }
45     endTimer("blockActivations   ")
46   end
48   def testInstantiations
49     beginTimer
50     ($oneMillion/8).times {
51       Tester.new; Tester.new; Tester.new; Tester.new;
52       Tester.new; Tester.new; Tester.new; Tester.new;
53     }
54     endTimer("instantiations     ")
55   end
57   def testLocals
58     beginTimer
59     v = 1
60     ($oneMillion/8).times {
61       v; v; v; v;
62       v; v; v; v;
63     }
64     endTimer("localAccesses      ")
65   end
67   def testSetLocals
68     beginTimer
69     v = 1
70     ($oneMillion/8).times {
71       v = 1; v = 2; v= 3; v= 4;
72       v = 1; v = 2; v= 3; v= 4;
73     }
74     endTimer("localSets          ")
75   end
77   def test
78     puts ""
79     testLocals
80     testSetLocals
81     puts ""
82     testSlot
83     testSetSlot
84     puts ""
85     testBlock
86     testInstantiations
87     printf "Ruby version := \"%s\"", RUBY_VERSION
88     puts ""
89     puts "// values in millions per second"
90     puts ""
91   end
92 end
94 begin
95   Tester.new.test
96 end