Skip to content

Instantly share code, notes, and snippets.

@h3ik0th
Created October 15, 2021 16:04
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 h3ik0th/021115640331dfa91909864e455e9a48 to your computer and use it in GitHub Desktop.
Save h3ik0th/021115640331dfa91909864e455e9a48 to your computer and use it in GitHub Desktop.
# comprehensions vs lambda-filters
t = time.perf_counter()
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
strLamb = filter(lambda d: ((d.endswith("urday") or d.endswith("unday")) and "Oc" in d), datstrlist)
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
tLamb = time.perf_counter() - t
print(f'{tLamb:.4f} sec')
t = time.perf_counter()
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
c = [d for d in datstrlist if ((d.endswith("urday") or d.endswith("unday")) and "Oc" in d)]
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
tComp = time.perf_counter() - t
print(f'{tComp:.4f} sec: comprehension vs lambda-filter: {100*(tComp/tLamb-1):.1f}%')
df = pd.DataFrame(list(zip(strLamb,c)), columns=("lambda","comprehension"))
df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment