Skip to content

Instantly share code, notes, and snippets.

@yuwash
Created January 5, 2021 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuwash/f45848fd5d29b9aab8d91717b72f950a to your computer and use it in GitHub Desktop.
Save yuwash/f45848fd5d29b9aab8d91717b72f950a to your computer and use it in GitHub Desktop.
Benchmark for empty lists
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,
)
@yuwash
Copy link
Author

yuwash commented Jan 5, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment