Perhaps this is not so bad: if you list files and process each file name as you see it, your OS will most likely have a directory entry in the disk cache. And for practical purposes, the disk cache is O (1).
What will kill you is a mechanical hard drive. You will have 5 million disk accesses, each of which takes ~ 1/100 of a second. This is 50,000 seconds, more than half a day. This is the challenge that screams for SSD.
source share