-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuildstrings.py
More file actions
29 lines (23 loc) · 755 Bytes
/
buildstrings.py
File metadata and controls
29 lines (23 loc) · 755 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
import time
amount = 1_000_000
print("Compare time needed to build a string using concatenation and\nusing a list (append en join).")
print(f"Number of iterations: {amount}.\n")
start_time = time.time()
final_string = ''
for i in range(amount):
final_string += 'spam '
#print(final_string)
print()
end_time = time.time()
duration = str(round(end_time - start_time, 3))
print(f"Time needed using concatenation: {duration} seconds\n")
start_time = time.time()
final_string = []
for i in range(amount):
final_string.append('spam ')
final_string = ''.join(final_string)
#print(final_string)
print()
end_time = time.time()
duration = str(round(end_time - start_time, 3))
print(f"Time needed using a list (append and join): {duration} seconds\n")