Merge branch 'master' of git://iolanguage.com/Io
[io/quag.git] / samples / speed / speed.py
blobb3f3635a4934ae2de4ce4ed43f5a71db04e70434
1 import sys
2 import time
4 oneMillion = 1000000
6 class Tester:
7 def foo(self):
8 return 1
10 def beginTimer(self):
11 self.t1 = time.time()
13 def endTimer(self, s):
14 mps = 1/(time.time() - self.t1)
15 print "Python %s := %0.2f" % (s, mps)
17 def testSlot(self):
18 self.beginTimer()
19 self.x = 1
20 for i in xrange(oneMillion/8):
21 self.x; self.x; self.x; self.x;
22 self.x; self.x; self.x; self.x;
23 self.endTimer("slotAccesses ")
25 def testSetSlot(self):
26 self.beginTimer()
27 self.x = 1
28 for i in xrange(oneMillion/8):
29 self.x = 1; self.x = 2; self.x = 3; self.x = 4;
30 self.x = 1; self.x = 2; self.x = 3; self.x = 4;
31 self.endTimer("slotSets ")
33 def testBlock(self):
34 self.beginTimer()
35 for i in xrange(oneMillion/8):
36 self.foo(); self.foo(); self.foo(); self.foo();
37 self.foo(); self.foo(); self.foo(); self.foo()
38 self.endTimer("blockActivations ")
40 def testInstantiations(self):
41 self.beginTimer()
42 for i in xrange(oneMillion/8):
43 Tester(); Tester(); Tester(); Tester();
44 Tester(); Tester(); Tester(); Tester();
45 self.endTimer("instantiations ")
47 def testLocals(self):
48 self.beginTimer()
49 v = 1
50 for i in xrange(oneMillion/8):
51 v; v; v; v;
52 v; v; v; v;
53 self.endTimer("localAccesses ")
55 def testSetLocals(self):
56 self.beginTimer()
57 v = 1
58 for i in xrange(oneMillion/8):
59 v = 1; v = 2; v = 3; v = 4;
60 v = 1; v = 2; v = 3; v = 4;
61 self.endTimer("localSets ")
63 def test(self):
64 print ""
65 self.testLocals()
66 self.testSetLocals()
67 print ""
68 self.testSlot()
69 self.testSetSlot()
70 print ""
71 self.testBlock()
72 self.testInstantiations()
74 import sys
75 print "Python version := \"%i.%i.%i %s %i\"" % sys.version_info
76 print ""
77 print "// values in millions per second"
78 print ""
81 Tester().test()