I have a program that iterates over several thousand PNG files on an SMB shared network drive (a 2TB Samsung 970 Evo+) and adds up their individual file sizes. Unfortunately, it is very slow. After profiling the code, it turns out 90% of the execution time is spent on one function:
filesize += os.path.getsize(png)
where each png variable is the filepath to a single PNG file (of the several thousands) in a for loop that iterates over each one obtained from glob.glob() (which, to compare, is responsible for 7.5% of the execution time).
The code can be found here: https://pastebin.com/SsDCFHLX
Clearly there is something about obtaining the filesize over the network that is extremely slow, but I'm not sure what. Is there any way I can improve the performance? It takes just as long using filesize += os.stat(png).st_size too.
When the PNG files are stored on the computer locally, the speed is not an issue. It specifically becomes a problem when the files are stored on another machine that I access over the local network with a gigabit ethernet cable. Both are running Windows 10.
[2022-08-21 Update]
I tried it again with a 10 gigabit network connection this time and noticed something interesting. The very first time I run the code on the network share, the profiler looks like this:
but if I run it again afterward, glob() takes up significantly less time while getsize() is about the same:
if I instead run this code with the PNG files stored on a local NVMe drive (WD SN750) rather than a newtwork drive, here's what the profiler looks like:
It seems like once it is run for a second time on the network share, something has gotten cached that allows glob() to run much faster on the network share, at around the same speed it would run at on the local NVMe drive. But getsize() remains extremely slow, about 1/10th of the speed as when local.
Can somebody help me understand these two points:
- Why is
getsize()so much slower on the network share? Is there something that can be done to speed it up? - Why is
glob()slow the first time on the network share but not when I run it again immediately afterward?



