-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrunningTimeOfSimpleProgram.py
More file actions
58 lines (39 loc) · 1.39 KB
/
runningTimeOfSimpleProgram.py
File metadata and controls
58 lines (39 loc) · 1.39 KB
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
# Program tries to identify the runtime of creating a list using four different ways
import timeit
from timeit import Timer
# create list using concatination
def test1():
l = []
for i in range(3000):
l = l + [i]
# create list using append
def test2():
l = []
for i in range(3000):
l.append(i)
# create list using list comprehension
def test3():
l = [i for i in range(3000)]
# create list using concat and generator
def test4():
l = []
for i in xrange(3000):
l = l + [i]
# create list using list range
def test5():
l = list(range(3000))
# create list using list xrange
def test6():
l = list(xrange(3000))
t1 = Timer("test1()", "from __main__ import test1")
print "time taken using concatination: ", t1.timeit(number=1000), "ms"
t2 = Timer("test2()", "from __main__ import test2")
print "time taken using append: ", t2.timeit(number=1000), "ms"
t3 = Timer("test3()", "from __main__ import test3")
print "time taken using comprehension: ", t3.timeit(number=1000), "ms"
t4 = Timer("test4()", "from __main__ import test4")
print "time taken using concatination and xrange: ", t4.timeit(number=1000), "ms"
t5 = Timer("test5()", "from __main__ import test5")
print "time taken using list range: ", t5.timeit(number=1000), "ms"
t6 = Timer("test6()", "from __main__ import test6")
print "time taken using list xrange: ", t6.timeit(number=1000), "ms"