Last active
February 7, 2022 05:48
-
-
Save ngshiheng/cefa74052bd2149af2d1761c8dfb6b8e to your computer and use it in GitHub Desktop.
When To Use Generators in Python https://jerrynsh.com/using-generators-in-python-the-why-the-what-and-the-when/
This file contains hidden or 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
# List Comprehension | |
# ------------------ | |
cProfile.run('sum([i * i for i in range(100_000_000)])') | |
# 5 function calls in 13.956 seconds | |
# Ordered by: standard name | |
# ncalls tottime percall cumtime percall filename:lineno(function) | |
# 1 8.442 8.442 8.442 8.442 <string>:1(<listcomp>) | |
# 1 0.841 0.841 13.956 13.956 <string>:1(<module>) | |
# 1 0.000 0.000 13.956 13.956 {built-in method builtins.exec} | |
# 1 4.672 4.672 4.672 4.672 {built-in method builtins.sum} | |
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | |
# Generator Expression | |
# -------------------- | |
cProfile.run('sum((i * i for i in range(100_000_000)))') | |
# 100000005 function calls in 22.996 seconds | |
# Ordered by: standard name | |
# ncalls tottime percall cumtime percall filename:lineno(function) | |
# 100000001 11.745 0.000 11.745 0.000 <string>:1(<genexpr>) | |
# 1 0.000 0.000 22.996 22.996 <string>:1(<module>) | |
# 1 0.000 0.000 22.996 22.996 {built-in method builtins.exec} | |
# 1 11.251 11.251 22.996 22.996 {built-in method builtins.sum} | |
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} |
This file contains hidden or 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 csv | |
def read_csv_from_generator_fn(): | |
with open('large_dataset.csv', 'r') as f: | |
reader = csv.reader(f) | |
for row in reader: | |
yield row | |
# To get the same output as result_1, | |
# We generate a list using our newly created Generator function: | |
result_2 = [row for row in read_csv_from_generator_fn()] | |
# Output same as result_1: | |
# [['a','b','c', ... ], ['x','y','z', ... ] ... ] |
This file contains hidden or 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 csv | |
def read_csv_from_regular_fn(): | |
with open('large_dataset.csv', 'r') as f: | |
reader = csv.reader(f) | |
return [row for row in reader] | |
result_1 = read_csv_from_regular_fn() | |
# Output: | |
# [['a','b','c', ... ], ['x','y','z', ... ] ... ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment