Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such a thing as "too many yield statements" in python?

Tags:

python

yield

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.

like image 463
saidimu apale Avatar asked Feb 27 '23 12:02

saidimu apale


1 Answers

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.

like image 129
recursive Avatar answered Mar 25 '23 08:03

recursive