-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuple and list, and str concat.py
82 lines (66 loc) · 2.04 KB
/
tuple and list, and str concat.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import time
from tqdm import tqdm
time_for_assing = []
time_for_not_ass = []
time_for_tuple = []
n = 250000
for _ in tqdm(range(100)):
# assign
start = time.time()
a = ('a '*n).split()
for _ in a:
pass
time_for_assing.append(time.time()-start)
# no assign
start = time.time()
for _ in ('a '*n).split():
pass
time_for_not_ass.append(time.time()-start)
# tuple
start = time.time()
for _ in tuple(('a '*n).split()):
pass
time_for_tuple.append(time.time()-start)
print('assigning test Starts...')
print(f'avg time_for_assing: {sum(time_for_assing)/len(time_for_assing)}')
print(f'avg time_for_not_ass: {sum(time_for_not_ass)/len(time_for_not_ass)}')
print(f'avg time_for_tuple: {sum(time_for_tuple)/len(time_for_tuple)}')
print('assigning test Ends...\n')
# purely test the difference btw list and tuple
time_for_t = []
time_for_l = []
n = 100000
for _ in tqdm(range(1000)):
start = time.time()
for i in tuple(range(n)):
pass
time_for_t.append(time.time()-start)
start = time.time()
for i in list(range(n)):
pass
time_for_l.append(time.time()-start)
print('purely test the difference btw list and tuple Starts...')
print(f'avg time_for_tuple: {sum(time_for_t)/len(time_for_t)}')
print(f'avg time_for_list: {sum(time_for_l)/len(time_for_l)}')
print('purely test the difference btw list and tuple Ends...\n')
# test different way to concat the str in python
time_for_t = []
time_for_l = []
n = 3000000
s = 'a'*n
for _ in tqdm(range(100)):
start = time.time()
tmp = ''
for ss in s:
tmp += ss
time_for_t.append(time.time()-start)
start = time.time()
s_list = []
for ss in s:
s_list.append(ss)
s_list = ''.join(s_list)
time_for_l.append(time.time()-start)
print('test different way to concat the str in python Starts...')
print(f'avg time_for_t: {sum(time_for_t)/len(time_for_t)}')
print(f'avg time_for_l: {sum(time_for_l)/len(time_for_l)}')
print('test different way to concat the str in python Ends...')