Quicker (quickest?) way to get number of files in a directory with over 200,000 files

前端 未结 10 1418
Happy的楠姐
Happy的楠姐 2021-02-04 07:35

I have some directories containing test data, typically over 200,000 small (~4k) files per directory.

I am using the following C# code to get the number of files in a di

10条回答
  •  醉梦人生
    2021-02-04 08:05

    The code you've got is slow because it first gets an array of all the available files, then takes the length of that array.

    However, you're almost certainly not going to find any solutions that work much faster than that.

    Why?

    Access controls.

    Each file in a directory may have an access control list - which may prevent you from seeing the file at all.

    The operating system itself can't just say "hey, there are 100 file entries here" because some of them may represent files you're not allowed to know exist - they shouldn't be shown to you at all. So the OS itself has to iterate over the files, checking access permissions file by file.

    For a discussion that goes into more detail around this kind of thing, see two posts from The Old New Thing:

    • Why doesn't the file system have a function that tells you the number of files in a directory?
    • Why doesn't Explorer show recursive directory size as an optional column?

    [As an aside, if you want to improve performance of a directory containing a lot of files, limit yourself to strictly 8.3 filenames. No I'm not kidding - it's faster, because the OS doesn't have to generate an 8.3 filename itself, and because the algorithm used is braindead. Try a benchmark and you'll see.]

提交回复
热议问题