import time class bar def __init__ self self self def oblah self retur

 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
import time
class bar:
def __init__(self, a, b):
self.a = a
self.b = b
def oblah(self):
return self.a+self.b
def fblah(a,b):
return a+b
arrays = []
times = 200000
# Arrays
# Create
arr_create = time.time()
for i in xrange(times):
arrays.append({'a':1, 'b': 3})
arr_create = arr_create - time.time()
# Get
arr_get = time.time()
for i in xrange(times):
tmp = arrays[i]['a']
tmp = arrays[i]['b']
arr_get = arr_get - time.time()
# Function
arr_f = time.time()
for i in xrange(times):
tmp = fblah(arrays[i]['a'], arrays[i]['b'])
arr_f = arr_f - time.time()
# Clean
arrays = []
# Objects
objects = []
# Create
obj_create = time.time()
for i in xrange(times):
objects.append(bar(1,3))
obj_create = obj_create - time.time()
# Get
obj_get = time.time()
for i in xrange(times):
tmp = objects[i].a
tmp = objects[i].b
obj_get = obj_get - time.time()
# Function
obj_f = time.time()
for i in xrange(times):
tmp = objects[i].oblah()
obj_f = obj_f - time.time()
print "Arrays vs. Objects benchmark, iterations:"+str(times)
print "Create:"
print " Arrays: "+str(arr_create)
print " Objects: "+str(obj_create)
print "Get:"
print " Arrays: "+str(arr_get)
print " Objects: "+str(obj_get)
print "Function:"
print " Arrays: "+str(arr_f)
print " Objects: "+str(obj_f)