Faster file move method other than File.Move

前端 未结 3 2027
北恋
北恋 2021-02-09 02:39

I have a console application that is going to take about 625 days to complete. Unless there is a way to make it faster.

First off I am working in a directory that has a

3条回答
  •  渐次进展
    2021-02-09 03:36

    18 seconds isn't really unusual. NTFS does not perform well when you have a lot of files in a single directory. When you ask for a file, it has to do a linear search of its directory data structure. With 1,000 files, that doesn't take too long. With 10,000 files you notice it. With 4 million files . . . yeah, it takes a while.

    You can probably do this even faster if you pre-load all of the directory entries into memory. Then rather than calling the FileInfo constructor for each file, you just look it up in your dictionary.

    Something like:

    var dirInfo = new DirectoryInfo(path);
    // get list of all files
    var files = dirInfo.GetFileSystemInfos();
    var cache = new Dictionary();
    foreach (var f in files)
    {
        cache.Add(f.FullName, f);
    }
    

    Now when you get a name from the database, you can just look it up in the dictionary. That might very well be faster than trying to get it from the disk each time.

提交回复
热议问题