import time
def functionTest1(aCookies):
try:
integers = map(int, aCookies.get("integers").split(","))
return integers
except (ValueError, AttributeError):
# handle bad cookie
pass
def functionTest2(aCookies):
lIntsCookie = aCookies.get("integers")
if lIntsCookie is not None:
try:
integers = map(int, aCookies.get("integers").split(","))
return integers
except (ValueError, AttributeError):
# handle bad cookie
pass
def functionTest3(aCookies):
try:
integers = map(int, aCookies.get("integers").split(","))
except (ValueError, AttributeError):
integers = [1,2,3,4]
aCookies["integers"] = ",".join(map(str, integers))
return integers
def functionTest4(aCookies):
try:
integers = map(int, aCookies.get("integers").split(","))
return integers
except (ValueError, AttributeError):
#integers = [1,2,3,4]
#aCookies["integers"] = ",".join(map(str, integers))
pass
def testFunction(aFunction, aWithoutRightCookie):
lTimeStart = time.time()
if aWithoutRightCookie:
lCookiesObj = dict(foo=1, bar=2)
else:
lCookiesObj = dict(foo=1, integers='1,2')
for i in range(1,1000000):
aFunction(lCookiesObj)
lTimeFinish = time.time()
print '%s. time elapsed: %s' % (aFunction.__name__, lTimeFinish - lTimeStart)
testFunction(functionTest1, True)
testFunction(functionTest1, False)
testFunction(functionTest2, True)
testFunction(functionTest2, False)
testFunction(functionTest3, True)
testFunction(functionTest3, False)
testFunction(functionTest4, True)
testFunction(functionTest4, False)