简体   繁体   中英

FileInfo.move vs RoboCopy /MOV speed:efficiency

This question is partially coming from a curiosity standpoint, but also from a practical one.

The application I'm building has a need to copy files from a computer to a USB and back. For this we use robocopy, and don't intend to change that.

But prior to doing the copy action, we create a backup of the contents. For example:

  • Backup contents of E:\ into E:\USB_Backup
  • Copy from C:\SomeFolder to E:\

Or

  • Backup C:\Destination to C:\Destination\USB_Backup
  • Copy from E:\ to C:\Destination, ignoring 'USB_Backup' folder.

The copying of the files will be done with robocopy. But my concern is moving the files to the backup for this question. Robocopy seems to copy all files, then delete the source. Meanwhile windows explorer drag and drop is much quicker, because it just updates the paths of the files.

In that scenario, where the drive letter stays the same just the folder path is changed, would a loop that uses FileInfo.move() be quicker?

Alrighty, I finally have some time to perform and report the results of my testing here for anyone in the future who might have a similar question.

Doing a loop at the top-level if way faster.

USB2.0 drive was used for testing Source: D:\MoveSource
Dest: D:\MoveDest
194MB of files -> 2,696 Files, 87 Folders

Using RoboSharp to perform the move operation:

RoboSharp.RoboCommand rc = new RoboCommand(MoveSource, MoveDest, true);
rc.CopyOptions.MoveFilesAndDirectories = true;
rc.CopyOptions.MultiThreadedCopiesCount = 1;
rc.CopyOptions.CopySubdirectoriesIncludingEmpty = true;
rc.Start().Wait();

//Command: D:\Source D:\Dest "*.*" /COPY:DAT /DCOPY:DA /E /MOVE /MT:1 /R:0 /W:30 /V  /BYTES 
// Results: Directories: 88, Files: 2696, Bytes: 204282757, Speed: 1852148 Bytes/sec

Total elapsed time: 225,215ms (performed 1 run only because I know this operation is several minutes on average in my application using this method, so the result was well within expected)

Here was my code for a top-level move (one that doesn't dig into folder structure, compare or filter files, or log anything)

var dirs = Directory.GetDirectories(MoveSource);
var files = Directory.GetFiles(MoveSource);
string dest;
int i = 0;
long msTotal = 0;
while (i < 20)
{
    var SW = StopWatchStart();
    Directory.CreateDirectory(MoveDest);
    foreach (var d in files)
    {
        dest = Path.Combine(MoveDest, Path.GetFileName(d));
        Directory.Move(d, dest);
    }
    foreach (var d in dirs)
    {
        var D = new DirectoryInfo(d);
        dest = Path.Combine(MoveDest, D.Name);
        D.MoveTo(dest);
    }
    Directory.Delete(MoveSource, true);
    SW.Stop();
    msTotal += SW.ElapsedMilliseconds;
    int tries = 0;
    while (Directory.Exists(MoveDest) && tries < 30)
    {
        try
        {
            tries++;
            Task.Delay(350).Wait();
            Directory.Move(MoveDest, MoveSource);
        }
        catch { }
        }
    i++;
}

Average Time (20-run average): 973ms

Using a recursive loop to dig into the folder structure in the same manner RoboCopy does (which would then be able to be used for filtering/logging/etc if needed) had a average elapsed time of 38,499ms(20 run average), which is still 5x quicker than RoboCopy performed for the same test (granted I wasn't logging file sizes or generating any results data) ( I ran this test with an average because after 3 runs that were pretty quick compared to RoboCopy, but still had minor swings, I figured this would be a good thing to average).

So the results are:

  • RoboSharp ( RoboCopy ) -> 225,215ms
  • Recursive Routine that digs into the structure and uses FileInfo.MoveTo() -> 38,499ms (20 run average)
  • Directory.Move() that loops through the top-level folder's directories and files -> 973ms (20 run average)

Now since that was using a USB2.0 device, I'm likely going to re-run this test over a network drive before really implementing it into my application. But manually performing the loop is MUCH faster than robocopy, but you would have to add in all the scenarios robocopy can test for, which would then add in processing time. Since my application is a simple bulk move for backup purposes, the recursive loop will be much quicker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM