-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursionVloop_timeit.py
More file actions
68 lines (36 loc) · 985 Bytes
/
recursionVloop_timeit.py
File metadata and controls
68 lines (36 loc) · 985 Bytes
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
59
60
61
62
63
64
65
66
67
68
'''
function to call itself to count to 10
do this by recursion and not a for loop
'''
import timeit as tt
import numpy as np
# recursive function
def rec(n):
if n < 100:
# print('keep going', n)
rec(n+1)
else:
# print('REC ended here', n)
pass
return 1
return 1
# loop method
def loo(n):
for i in range(n, 100):
# print('keep going',i)
i = i * 2
pass
# print('LOO ended here', i)
return 1
# numpy loop method
def npl(n):
for i in np.arange(n,100):
i = i * 2
pass
return 1
x = tt.timeit('rec(0)',number=10000, globals=globals())
y = tt.timeit('loo(0)',number=10000, globals=globals())
z = tt.timeit('npl(0)',number=10000, globals=globals())
print('the timeit function for REC gets',x)
print('the timeit function for LOO gets',y)
print('the timeit function for NPL gets',z)