0

Whats the fastest way to get the size of a directory? Is multithreading possibile?

How can I find the size of all files located inside a folder?
Size of a directory

No other questions on stackoverflow & other sites mention multithreading but it should be possibile like if the directory contains other directories.. The other solutions on stackoverflow are pretty slow , slower even than
Windows Folder->Properties dialog even on Release... Tried all the solutions on stack-overflow and they are either not compiling or very slow...

1 Answers1

4

Whats the fastest way to get the size of a directory?

By "size of a directory", I suppose that you mean cumulative size of files in the directory and its descendants.

In general, the solution is to traverse the directory tree, get the size of each file, and accumulate.

There is no one solution that is fastest in all use cases. It always depends on details.

Is multithreading possibile?

Certainly. However, it's hard to guarantee that you would get better performance with multi-threading.

Problem is that the computational cost of the operation is trivial, while the bottleneck is reading the file system. If each thread is waiting for a storage device, then you might not get any speedup. In fact, if the storage device is a spinning disk, then parallel read will likely be much slower.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    unless the OS reorders parallel reads to find the fastest route across the disk – user253751 May 03 '22 at 17:16
  • ... and assuming the OS (including the drivers/modules) does not massively use locks like the insane [giant lock](https://en.wikipedia.org/wiki/Giant_lock). Unfortunately, as of 2022, this is still quite frequent in practice... – Jérôme Richard May 03 '22 at 19:42