使用fread / fwrite将文件复制到USB的性能 [英] Performance of copying a file with fread/fwrite to USB

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

问题描述

我在一块代码前面,它将一个文件复制到usb设备。
以下部分是重要的:

  while((bytesRead = fread(buf,1,16 * 1024, m_hSource))&&!bAbort){
//写入目标
long bytesWritten = fwrite(buf,1,bytesRead,m_hTarget);

m_lBytesCopied + = bytesWritten;

这个东西,客户说,与正常的pc - usb速度相比很慢。我没有编码,所以这是我的工作,优化。



所以我想知道,如果这是一个更好的方法,首先读完整的文件,然后写文件。但我不知道这是多么容易出错。
该代码还检查每个copystep后,如果所有字节写入正确,这样也可能会减慢的过程。



我不是那个c ++&硬件上师,所以我问你们,我怎么能加快速度,保持复制成功。

解决方案


  1. 尝试以大块读/写
  2. 如果只是要复制文件,您可以随时调用系统

  3. 此代码还会检查每个copystep之后是否所有字节

    您可以通过创建 hash 来检查它。

    的大块。就像将文件分割成64M块。然后匹配那些块的哈希值。


  4. 如果您有 mmap MapViewOfFile 可用,先映射文件。然后写它到usb。这样,读操作将由内核处理


  5. Kerrek刚刚评论过在 mmap 上使用 memcpy memcpy 与2 mmap ed档案似乎不错。

另请注意,大多数最近的操作系统在删除时会写入USB记忆棒。在删除之前,它只是将数据写入缓存。因此,从操作系统复制可能会更快。


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天全站免登陆