-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_interning_performance.py
More file actions
38 lines (34 loc) · 1.29 KB
/
string_interning_performance.py
File metadata and controls
38 lines (34 loc) · 1.29 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
from sys import intern
import random as rd
import time
from string import ascii_letters
str_lens = (100, 10_000, 5_000_000)
loops = (100, 10_000, 1_000_000)
for str_len in str_lens:
rand_str = ''.join(rd.choices(ascii_letters + ' ', k=str_len))
str1 = rand_str + ' The End. '
str2 = rand_str + ' The End. '
str3 = intern(rand_str + ' The End. ')
str4 = intern(rand_str + ' The End. ')
# print(f'{(id(str3) == id(str4)) = }')
# print(f'{(id(str1) == id(str2)) = }')
for loop in loops:
time1 = time.time()
for _ in range(loop):
is_equal_sans_interning = (str1 == str2)
time2 = time.time()
duration_sans_interning = time2 - time1
print(f'{str_len = :<12}{loop = :<12}')
print(f'{duration_sans_interning = }')
# ================================
time3 = time.time()
for _ in range(loop):
is_equal_with_interning = (str3 == str4)
time4 = time.time()
duration_with_interning = time4 - time3
print(f'{duration_with_interning = }')
# print()
print(f'{(duration_with_interning < duration_sans_interning) = }')
print(f'{(duration_with_interning / duration_sans_interning) = :.10f}')
print('------')
print('===========================')