Alt*_*tar 8 c# windows performance copy
我正在尝试实现一个filecopy方法,该方法可以与使用Windows资源管理器完成的副本的性能相匹配.
例如,从我们的nas到我的计算机的副本(使用Windows资源管理器),执行超过100mb /秒.
我当前的实现以大约55mb/sec的速度执行相同的副本,这已经比以29mb/sec执行的System.IO.File.Copy()更好.
static void Main(string[] args)
{
String src = @"";
String dst = @"";
Int32 buffersize = 1024 * 1024;
FileStream input = new FileStream(src, FileMode.Open, FileAccess.Read, FileShare.None, 8, FileOptions.Asynchronous | FileOptions.SequentialScan);
FileStream output = new FileStream(dst, FileMode.CreateNew, FileAccess.Write, FileShare.None, 8, FileOptions.Asynchronous | FileOptions.SequentialScan);
Int32 readsize = -1;
Byte[] readbuffer = new Byte[buffersize];
IAsyncResult asyncread;
Byte[] writebuffer = new Byte[buffersize];
IAsyncResult asyncwrite;
DateTime Start = DateTime.Now;
output.SetLength(input.Length);
readsize = input.Read(readbuffer, 0, readbuffer.Length);
readbuffer = Interlocked.Exchange(ref writebuffer, readbuffer);
while (readsize > 0)
{
asyncwrite = output.BeginWrite(writebuffer, 0, readsize, null, null);
asyncread = input.BeginRead(readbuffer, 0, readbuffer.Length, null, null);
output.EndWrite(asyncwrite);
readsize = input.EndRead(asyncread);
readbuffer = Interlocked.Exchange(ref writebuffer, readbuffer);
}
DateTime Stop = DateTime.Now;
TimeSpan Duration = Stop - Start;
Double speed = input.Length / Duration.TotalSeconds; // bytes/s
System.Console.WriteLine("MY Speed : " + (speed / 1024 / 1024).ToString() + " mo/sec");
input.Close();
output.Close();
System.IO.File.Delete(dst);
}
Run Code Online (Sandbox Code Playgroud)
知道如何提高性能吗?
编辑:
该文件是从基于linux的nas读取的,带有一个10千兆以太网接口,后面有60个驱动器(不用担心它的性能,它工作得很好)并写入本地raid0,它可以写入大约140MB的数据/秒.
瓶颈是目标的千兆网络接口,我无法使用当前代码到达.
此外,删除写入不会使读取更快,所以我不能超过这55MB /秒的读取限制.
编辑2:
速度问题与源文件存储在网络共享上的事实有关.只使用我的代码从本地驱动器读取,我的速度为112MB /秒.
编辑3:
Samba似乎不是问题.我在我的linux nas上用nfs共享替换了cifs share(samba),并且在我的win7客户端上获得了比samba更糟糕的结果.
使用nfs,我的复制方法和Windows资源管理器具有相同的性能,大约42MB /秒.
我没有想法......
编辑4:
只是为了确保Windows是问题,我已经安装了一个debian lenny,安装了我的nas nrough nfs并在单声道下使用相同的代码获得79MB /秒.
尝试将缓冲区大小更改为等于硬盘上的扇区大小 - 可能是4Kb.还可以使用System.Diagnostics.Stopwatch类进行计时.
我也不打算在紧密循环中使用异步方法 - 它会产生一些开销,并从池中分配一个线程来完成工作.
同样,也可以使用该using语句来管理流的处理.但请注意,这会使您的定时偏差,因为您在停止定时器后正在处理对象.
可能唯一更快的选择是使用带有 2-6 MB 缓冲区标志的unbuffered IOReadFile Function、CreateFile Function、WriteFile FunctionFILE_FLAG_NO_BUFFERING。
此外,您还必须将缓冲区大小与文件系统扇区大小等对齐。
它会明显更快——尤其是在 Windows XP 中。
顺便提一句。通过这种方式,我在条带 RAID 0 阵列上实现了约 400 MB 的带宽(使用 4MB 缓冲区)。