Created
January 5, 2021 10:34
-
-
Save yuwash/f45848fd5d29b9aab8d91717b72f950a to your computer and use it in GitHub Desktop.
Benchmark for empty lists
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
import argparse | |
def create_data(amount=50): | |
return [ | |
{"content": "abcdefghijkl"} for __ in range(amount) | |
] | |
def create_data_empty_list(amount=50): | |
return [ | |
{"content": "abcdefghijkl", "children": []} for __ in range(amount) | |
] | |
def main(empty_list, iterate_empty, repeat=1000000): | |
data = create_data_empty_list() if empty_list else create_data() | |
for __ in range(repeat): | |
for item in data: | |
children = item.get("children") | |
if empty_list and iterate_empty: | |
[True for item_ in children] | |
elif children is None: | |
continue | |
if __name__ == "__main__": | |
argparser = argparse.ArgumentParser() | |
argparser.add_argument("--empty-list", action="store_true") | |
argparser.add_argument("--iterate-empty", action="store_true") | |
parsed_args = argparser.parse_args() | |
main( | |
empty_list=parsed_args.empty_list, | |
iterate_empty=parsed_args.iterate_empty, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result
$ time python benchmark.py
real 0m3,172s
user 0m3,160s
sys 0m0,004s
$ time python benchmark.py --empty-list
real 0m3,351s
user 0m3,329s
sys 0m0,004s
$ time python benchmark.py --empty-list --iterate-empty
real 0m10,840s
user 0m10,802s
sys 0m0,008s