From fe1b482fbadcf8944e8e1dbf3d23740aab1e3522 Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Tue, 24 May 2022 21:58:00 -0700 Subject: [PATCH] Add benchmark comparing join of generator expression vs join of list comprehension --- bench_comprehensions.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bench_comprehensions.py b/bench_comprehensions.py index 8a5f103..7a24731 100644 --- a/bench_comprehensions.py +++ b/bench_comprehensions.py @@ -9,7 +9,17 @@ def filter_list_as_comprehension(): inputs = range(100_000) result = [i for i in inputs if i % 2] +def join_generator_expression(): + words = ['data', 'type', 'is', 'so', 'long', 'now'] + for x in range(100_000): + ''.join(ele.title() for ele in words) + +def join_list_comprehension(): + words = ['data', 'type', 'is', 'so', 'long', 'now'] + for x in range(100_000): + ''.join([ele.title() for ele in words]) __benchmarks__ = [ (filter_list_as_loop, filter_list_as_comprehension, "Using a list comprehension to filter another list"), -] \ No newline at end of file + (join_generator_expression, join_list_comprehension, "Join list comprehension instead of generator expression"), +]