scandir slower than listdir

Tim Delaney timothy.c.delaney at gmail.com
Thu Jul 20 20:06:51 EDT 2017


On 20 July 2017 at 21:43, Skip Montanaro <skip.montanaro at gmail.com> wrote:

> scandir returns an iterator of DirEntry objects which contain more
> > information than the mere name.
> >
>
> As I recall, the motivation for scandir was to avoid subsequent system
> calls, so it will be slower than listdir the way you've tested it. If you
> add in the cost of fetching the other bits Terry mentioned, I suspect your
> relative timing will change.
>

In addition, listdir() returns a list of names, so building a new list from
that is fairly fast (can use a single allocation of the correct size).
scandir() returns an iterator, so building a list from that may require
multiple reallocations (depending on the number of entries in the
directory), which could skew the test results.

In neither case is building a list from the result the way you would
normally use it. A more accurate test of the way both functions would
normally be used would be to iterate over the results instead of eagerly
building a list. In this test you would also expect scandir() to use less
memory for a large directory.

Tim Delaney



More information about the Python-list mailing list