import time
import random
class classA:
def __init__(self):
self.doThing1()
self.doThing2()
def doThing1(self):
# create 100,000 new class attributes and assign a value of something other than 1,000
for i in range (0,100000):
name = "attribute" + str(i)
value = random.randint(0,1000)
if value == 1000:
value = 999
setattr(self,name,value)
def doThing2(self):
# 100,000 times, look up a random atribute and change its value to 1,000
for i in range (0,100000):
name = "attribute" + str(random.randint(0,10000))
setattr(self,name,1000)
class classB:
def __init__(self):
self.attribute = {}
self.doThing1()
self.doThing2()
def doThing1(self):
# create a single one-level nested dictionary attribute with 100,000 entries and assign
# a value of something other than 1,000 to each
for i in range (0,100000):
name = str(i)
value = random.randint(0,1000)
if value == 1000:
value = 999
self.attribute[name] = value
def doThing2(self):
# 100,000 times, look up a random key in the dict attribute and change its value to 1,000
for i in range (0,100000):
name = str(random.randint(0,10000))
self.attribute[name] = 1000
class classC:
def __init__(self):
self.doThing1()
self.doThing2()
def doThing1(self):
# create 100,000 new class attributes and assign a value of something other than 1,000 to each
# (Simpler ways to do this (ex: classA), but did it this way to keep consistent with classD
for i1 in range (0,10):
for i2 in range(0, 10):
for i3 in range(0, 10):
for i4 in range(0, 10):
for i5 in range(0, 10):
name = "attribute"+str(i1)+str(i2)+str(i3)+str(i4)+str(i5)
value = random.randint(0,1000)
if value == 1000:
value = 999
setattr(self,name,value)
def doThing2(self):
# 100,000 times, look up a random class attribute and change its value to 1,000
for i1 in range (0,10):
tier1 = str(random.randint(0,9))
for i2 in range(0, 10):
tier2 = str(random.randint(0, 9))
for i3 in range(0, 10):
tier3 = str(random.randint(0, 9))
for i4 in range(0, 10):
tier4 = str(random.randint(0, 9))
for i5 in range(0, 10):
tier5 = str(random.randint(0, 9))
name = "attribute" + str(tier1)+str(tier2)+str(tier3)+str(tier4)+str(tier5)
setattr(self,name,1000)
class classD:
def __init__(self):
self.attribute = {}
self.doThing1()
self.doThing2()
def doThing1(self):
# create a single 5-level nested dictionary attribute with 100,000 entries and assign
# a value of something other than 1,000 to each
for i1 in range (0,10):
self.attribute[str(i1)] = {}
for i2 in range (0,10):
self.attribute[str(i1)][str(i2)] = {}
for i3 in range(0, 10):
self.attribute[str(i1)][str(i2)][str(i3)] = {}
for i4 in range(0, 10):
self.attribute[str(i1)][str(i2)][str(i3)][str(i4)] = {}
for i5 in range(0, 10):
value = random.randint(0,1000)
if value == 1000:
value = 999
self.attribute[str(i1)][str(i2)][str(i3)][str(i4)][str(i5)] = value
def doThing2(self):
# 100,000 times, look up a random key in the nested dict attribute and change its value to 1,000
for i1 in range (0,10):
tier1 = str(random.randint(0,9))
for i2 in range(0, 10):
tier2 = str(random.randint(0, 9))
for i3 in range(0, 10):
tier3 = str(random.randint(0, 9))
for i4 in range(0, 10):
tier4 = str(random.randint(0, 9))
for i5 in range(0, 10):
tier5 = str(random.randint(0, 9))
self.attribute[tier1][tier2][tier3][tier4][tier5] = 1000
classAruntimes = []
classBruntimes = []
classCruntimes = []
classDruntimes = []
iters = 10
for iteration in range(0,iters):
start = time.time()
for i in range (0,10):
myStuff = classA()
runtime = round(time.time()-start,5)
classAruntimes.append(runtime)
start = time.time()
for i in range (0,10):
myStuff = classB()
runtime = round(time.time()-start,5)
classBruntimes.append(runtime)
start = time.time()
for i in range(0, 10):
myStuff = classC()
runtime = round(time.time() - start, 5)
classCruntimes.append(runtime)
start = time.time()
for i in range(0, 10):
myStuff = classD()
runtime = round(time.time() - start, 5)
classDruntimes.append(runtime)
print("Iteration",(iteration+1),"of",iters,"complete.")
def avg(timeList):
avg = 0
for value in timeList:
avg += value
avg = avg/len(timeList)
return round(avg,5)
print("class A runtimes:", classAruntimes, "average:",avg(classAruntimes))
print("class B runtimes:", classBruntimes, "average:",avg(classBruntimes))
print("class C runtimes:", classCruntimes, "average:",avg(classCruntimes))
print("class D runtimes:", classDruntimes, "average:",avg(classDruntimes))
Friday, April 23, 2021
Python dictionary Vs class variables | All About Circuits - All About Circuits - Dictionary
Labels:
Dictionary
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment