c++ - 合并没有空格的文件比有空格更快

标签 c++ file visual-c++ merge

因此,我创建了一个用于合并文件的 C++ 可执行文件。我有 43 个文件,每个文件大小为 100MB。所以总共约4.3GB。

两种情况:

一:如果文件名是1, 2, 3, 4, 5, 6, ..., 43 大概需要2分钟完成合并。

二:如果文件名为 This File.ova0, This File.ova1, ..., This File.ova42 则需要大约 7 分钟才能完成合并。

这是同一个文件,我只是重命名了文件。知道出了什么问题吗?

这是c++代码

#include <iostream>
#include <fstream>

#include <vector>
#include <string>

#include "boost/filesystem.hpp"

namespace bfs = boost::filesystem;

#pragma warning(disable : 4244)


typedef std::vector<std::string> FileVector;
int main(int argc, char **argv)
{

    int bucketSize = 3024 * 3024;

    FileVector Files;

    //Check all command-line params to see if they exist..
    for(int i = 1; i < argc; i++)
    {
        if(!bfs::exists(argv[i]))
        {
            std::cerr << "Failed to locate required part file: " << argv[i] << std::endl;
            return 1;
        }

        //Store this file and continue on..
        std::cout << "ADDING " << argv[i] << std::endl;
        Files.push_back(argv[i]);
    }

    //Prepare to combine all the files..
    FILE *FinalFile = fopen("abc def.ova", "ab");

    for(int i = 0; i < Files.size(); i++)
    {
        FILE *ThisFile = fopen(Files[i].c_str(), "rb");     

        char *dataBucket = new char[bucketSize];

        std::cout << "Combining " << Files[i].c_str() << "..." << std::endl;

        //Read the file in chucks so we do not chew up all the memory..
        while(long read_size = (fread(dataBucket, 1, bucketSize, ThisFile)))
        {
            //FILE *FinalFile = fopen("abc def.ova", "ab");
            //::fseek(FinalFile, 0, SEEK_END);
            fwrite(dataBucket, 1, read_size, FinalFile);
            //fclose(FinalFile);
        }

        delete [] dataBucket;
        fclose(ThisFile);
    }
    fclose(FinalFile);

    return 0;
}

我像这样通过 .bat 文件运行它:

@ECHO OFF

Combiner.exe "This File.ova0" "This File.ova1" "This File.ova2" 

PAUSE

@ECHO OFF

Combiner.exe 1 2 3

PAUSE

两个.bat文件都到文件名末尾,我这里只写了3个文件,不然会太长

谢谢

最佳答案

默认情况下,Windows 缓存从磁盘读取和写入磁盘的文件数据。这意味着读取操作从系统内存中称为系统文件缓存的区域读取文件数据,而不是从物理磁盘读取文件数据。相应地,写操作将文件数据写入系统文件缓存而不是磁盘,这种类型的缓存称为回写缓存。缓存是按文件对象管理的: 更多信息:File Caching

关于c++ - 合并没有空格的文件比有空格更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14244842/

相关文章:

c++ - LPSolve C++源码库和IDE给出不同的LP任务求解结果

c++ - Boost::Python 中的智能指针转换

c++ - 部分模板特化类型折叠规则

c++ - 是否值得在生产中使用 std::tr1 ?

c++ - 在 C++ 模板中使用方法

file - 在 Windows Azure 上托管文件下载

c# - 在 C# 中创建对象和对象序列化

ruby - 如何在 ruby​​ 中使用 File.Open 打开存储在变量中的文件?

c++ - 获取共享缓存的逻辑 CPU 内核数(L1、L2、L3)

c++ - 为什么VC++中的程序会出现这个正则表达式 "freeze"?