-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
234c626
commit ddb56ae
Showing
3 changed files
with
32 additions
and
1 deletion.
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
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
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,27 @@ | ||
# Timing code in Python | ||
|
||
Using `timeit` to compare merging dictionaries in #Python: Old way with `**` unpacking vs. new way with `|`. | ||
|
||
`timeit` runs the code 1000 times, using `globals` to pass variables, and prints the timing results: | ||
|
||
``` | ||
import timeit | ||
dict1 = {f'key{i}': i for i in range(10000)} | ||
dict2 = {f'key{i}': i for i in range(5000, 15000)} | ||
old_way_time = timeit.timeit(''' | ||
merged_dict = {**dict1, **dict2} | ||
''', globals=globals(), number=1000) | ||
new_way_time = timeit.timeit(''' | ||
merged_dict = dict1 | dict2 | ||
''', globals=globals(), number=1000) | ||
print(f"Old way time: {old_way_time:.6f} seconds") | ||
print(f"New way time: {new_way_time:.6f} seconds") | ||
# Old way time: 0.222962 seconds | ||
# New way time: 0.222384 seconds | ||
``` | ||
|
||
#timeit |