forked from kumaya/python-programs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunningTimeOfSimpleProgram.py
More file actions
Latest commit
58 lines (39 loc) · 1.39 KB
/
Copy pathrunningTimeOfSimpleProgram.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
importtimeit
fromtimeitimportTimer
# create list using concatination
deftest1():
l= []
foriinrange(3000):
l=l+ [i]
# create list using append
deftest2():
l= []
foriinrange(3000):
l.append(i)
# create list using list comprehension
deftest3():
l= [iforiinrange(3000)]
# create list using concat and generator
deftest4():
l= []
foriinxrange(3000):
l=l+ [i]
# create list using list range
deftest5():
l=list(range(3000))
# create list using list xrange
deftest6():
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"