-
Notifications
You must be signed in to change notification settings - Fork 298
/
speed.py
executable file
·86 lines (70 loc) · 1.97 KB
/
speed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys
import time
oneMillion = 1000000
if sys.version_info >= (3,):
def xrange(v):
return range(int(v))
class Tester:
def foo(self):
return 1
def beginTimer(self):
self.t1 = time.time()
def endTimer(self, s):
mps = 1/(time.time() - self.t1)
print("Python %s := %0.2f" % (s, mps))
def testSlot(self):
self.beginTimer()
self.x = 1
for i in xrange(oneMillion/8):
self.x; self.x; self.x; self.x;
self.x; self.x; self.x; self.x;
self.endTimer("slotAccesses ")
def testSetSlot(self):
self.beginTimer()
self.x = 1
for i in xrange(oneMillion/8):
self.x = 1; self.x = 2; self.x = 3; self.x = 4;
self.x = 1; self.x = 2; self.x = 3; self.x = 4;
self.endTimer("slotSets ")
def testBlock(self):
self.beginTimer()
for i in xrange(oneMillion/8):
self.foo(); self.foo(); self.foo(); self.foo();
self.foo(); self.foo(); self.foo(); self.foo()
self.endTimer("blockActivations ")
def testInstantiations(self):
self.beginTimer()
for i in xrange(oneMillion/8):
Tester(); Tester(); Tester(); Tester();
Tester(); Tester(); Tester(); Tester();
self.endTimer("instantiations ")
def testLocals(self):
self.beginTimer()
v = 1
for i in xrange(oneMillion/8):
v; v; v; v;
v; v; v; v;
self.endTimer("localAccesses ")
def testSetLocals(self):
self.beginTimer()
v = 1
for i in xrange(oneMillion/8):
v = 1; v = 2; v = 3; v = 4;
v = 1; v = 2; v = 3; v = 4;
self.endTimer("localSets ")
def test(self):
print("")
self.testLocals()
self.testSetLocals()
print("")
self.testSlot()
self.testSetSlot()
print("")
self.testBlock()
self.testInstantiations()
import sys
print("Python version := \"%i.%i.%i %s %i\"" % tuple(sys.version_info))
print("")
print("// values in millions per second")
print("")
Tester().test()