Sorting is O (n lg N), so your list of 40,000 files will need about 600,000 operations (comparisons). If it takes about 20 seconds, that's about 30,000 comparisons per second. Therefore, each comparison takes about 100,000 measures. This is not possible due to processor processing. Sorting is almost certainly related to I / O binding, not CPU binding. Disk queries are especially expensive.
You may be able to reduce time by using multithreading to reduce the impact of disk accesses. That is, having several reads in the queue and expecting the drive to provide its data. To do this, use a (parallel) card that displays the file names during modification and populates this card with multiple threads. Then try using the sort method rather than using File.lastModified() .
Even if you populate this map with only one thread, you will get little benefit, because your sorting method will use local caching time, rather than request O / S each time for modification time. The advantage of such caching can be small, since O / S itself can cache this information.
source share