My app scans part of a file system, and my users reported it was very slow when they were scanning a network drive. Testing my code, I identified the bottleneck: the methods File.isFile()
, File.isDirectory()
, and File.isHidden()
, which are all calling fs.getBooleanAttributes(File f)
. This method appears to be very slow on Windows network drives. How can I improve performance? Can I avoid calling this method in some way?
Defensive code oftencalls those isXYZ()
methods, and it's generally good practise. However, sometimes the performance is poor, as you've discovered.
An alternative approach is to assume that the file is a file, it exists, it's visible, readable, etc, and just try and read it. If it isn't those things, you'll get an exception, which you can catch, and then do the checks to find out exactly what went wrong. That way, you're optimising for the common case (i.e. everything's fine), and only perform the slow operations when things go wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With