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. constructing fileinfo 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

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -