c# - Directory.GetFiles() performance issues -
using system.io.directory.getfiles()
, find images .png
extension located on nas server.
string searchingstring = "zllk9"; // original var filelist1= directory.getfiles(directorypath).select(p => new fileinfo(p)).where(q => q.name.substring(0, q.name.lastindexof('.')).split('_').first() == searchingstring); // fixed var filelist2 = directory.getfiles(directorypath, string.format("{0}_*.png", searchingstring));
there 2 ways find out files contain "zllkk9" words.
the first 'original' way using linq slow find out files. performance issues don't know different 'fixed' way?
i need understanding difference 2 ways carefully.
the first way slow 2 reasons:
you're constructing
fileinfo
object each file. there's no need if want file name. constructingfileinfo
relatively light, it's unnecessary , instantiations slow down if you're querying lot of files. since need file's name, can without step.the linq approach retrieves everything, filters afterwards. it's more efficient (and faster) file system filtering you.
if still want use linq, here's more performant version of query, cuts out lot of enumeration , string manipulation:
var filelist1 = directory.getfiles(directorypath).where( path => regex.ismatch(path.getfilename(path), @"^zllk9_.*\.png$"));
Comments
Post a Comment