I wrote two tests in a module (call it module1):
Expand|Select|Wrap|Line Numbers
- def listAppendTest(n):
- aList = []
- for i in range(n):
- aList.append(i)
- def listInitTest(n):
- aList = [0] * n
- for i in range(n):
- aList[i] = 1
Expand|Select|Wrap|Line Numbers
- import timeit
- test1ExecStr = \
- """from module1 import listAppendTest
- listAppendTest(5)"""
- t = timeit.Timer(test2ExecStr)
- print t.timeit()
- # 22.7464275488
- test2ExecStr = \
- """from module1 import listInitTest
- listInitTest(5)"""
- t = timeit.Timer(test2ExecStr)
- print t.timeit()
- # 17.7503707873