-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ POC to compare it
- Loading branch information
Showing
2 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import timeit | ||
from collections import defaultdict | ||
|
||
|
||
def dict_performance(n): | ||
regular_dict = {} | ||
for i in range(n): | ||
if i not in regular_dict: | ||
regular_dict[i] = [] | ||
regular_dict[i].append(i) | ||
|
||
|
||
def defaultdict_performance(n): | ||
default_dict = defaultdict(list) | ||
for i in range(n): | ||
default_dict[i].append(i) | ||
|
||
|
||
if __name__ == '__main__': | ||
n = 3000000 # Number of iterations | ||
dict_time = timeit.timeit(lambda: dict_performance(n), number=10) | ||
defaultdict_time = timeit.timeit(lambda: defaultdict_performance(n), number=10) | ||
|
||
print(f'Time taken by dict: {dict_time:.6f} seconds') | ||
print(f'Time taken by defaultdict: {defaultdict_time:.6f} seconds') | ||
|
||
# Sample output: | ||
# Time taken by dict: 4.709627 seconds | ||
# Time taken by defaultdict: 5.102884 seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters