If doing a directory listing and reading the files within, at what point does the performance of yield start to deteriorate, compared to returning a list of all the files in the directory?
Here I'm assuming one has enough RAM to return the (potentially huge) list.
PS I'm having problems inlining code in a comment, so I'll put some examples in here.
def list_dirs_list():
# list version
return glob.glob(/some/path/*)
def list_dirs_iter():
# iterator version
return glob.iglob(/some/path/*)
Behind the scenes both calls to glob use os.listdir so it would seem they are equivalent performance-wise. But this Python doc seems to imply glob.iglob is faster.
There is no point at which further use of yield
results in decreased performance. In fact, as compared to assembling things in a list, yield
actually improves by comparison the more elements there are.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With