复制一个文件用fread / fwrite来USB性能 [英] Performance of copying a file with fread/fwrite to USB

查看:156
本文介绍了复制一个文件用fread / fwrite来USB性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一块code的,前面这将文件复制到USB设备。
以下部分是重要的:

 ,而((读取动作= FREAD(BUF,1,16 * 1024,m_hSource))及和放大器;!bAbort){
    //写入到目标
    长bytesWritten = FWRITE(BUF,1,读取动作,m_hTarget);    m_lBytesCopied + = bytesWritten;

事情,客户说,这是pretty相较于普通PC&LT慢; - > USB速度。我没code这一点,所以这是我的工作,以优化。

所以我想知道,如果它是一个更好的办法,首先阅读完整的文件,然后写入文件中的一个步骤。但我不知道该怎么容易出错,这将是。
在code也检查各copystep后,如果正确的地方写,让所有的字节也可能会拖慢进程。

我不认为C ++和放大器;硬件大师,所以我问你们,我怎么能加快速度,并保持复制成功。


解决方案

  1. 尝试为读/写大块。 16M,32M​​是不坏的复制文件。

  2. 如果您只是要复制你总是可以的调用系统() 这将是更快的文件


  3.   

    在code也检查各copystep后,如果正确的地方写的所有字节,这样也可能会拖慢进程。


    您可以用更大的块的创建检查。像文件拆分为64M块。然后匹配块的哈希值。 Bittorrent协议有这个功能。


  4. 如果你的 MMAP MapViewOfFile 可用,首先映射文件。然后将其写入USB。这样的读操作会被内核处理。


  5. Kerrek只是评论了关于使用的memcpy MMAP 的memcpy 与2 MMAP 编辑文件似乎很大。

另外要注意的是,要被删除时,他们最近的操作系统写入到U盘。去除之前它只是在一个高速缓存中写入的数据。因此,从OS复制可能会出现更快。

I'm in front of a piece of code, which copies a file to a usb-device. Following part is the important one:

while((bytesRead = fread(buf, 1, 16*1024, m_hSource)) && !bAbort) {
    // write to target
    long bytesWritten = fwrite(buf, 1, bytesRead, m_hTarget);

    m_lBytesCopied += bytesWritten;

The thing, the customer said, it's pretty slow in comparison to normal pc<->usb speed. I didn't code this, so it's my job, to optimize.

So I was wondering, if it's a better approach to first read the complete file and then write the file in one step. But I don't know how error-prone this would be. The code also check after each copystep if all bytes where written correctly, so that might also slow down the process.

I'm not that c++ & hardware guru, so I'm asking you guys, how I could speed things up and keep the copying successful.

解决方案

  1. Try to read/write in big chunk. 16M, 32M are not bad for copying file.
  2. If you just want to copy the file you can always invoke system() It'll be faster.
  3. The code also check after each copystep if all bytes where written correctly, so that might also slow down the process.

    You can check it by creating hash of bigger chunk. Like splitting the file into 64M chunks. Then match hashes of those chunks. Bittorrent protocol has this feature.

  4. If you have mmap or MapViewOfFile available, map the file first. Then write it to usb. This way read operation will be handled by kernel.

  5. Kerrek just commented about using memcpy on mmap. memcpy with 2 mmaped file seems great.

Also note that, Most recent operating systems writes to USB stick when they are being removed. Before removal it just writes the data in a cache. So copy from OS may appear faster.

这篇关于复制一个文件用fread / fwrite来USB性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆