合并没有空间的文件比合并有空间的文件快

Merging File without space is faster than with space

本文关键字:空间 文件 合并 并没有      更新时间:2023-10-16

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

两种情况:

1:如果文件名为1,2,3,4,5,6,…, 43合并完成大约需要2分钟。

二:如果文件名是This file。ova0,这个文件。ova1,……,此文件。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缓存从磁盘读取并写入磁盘的文件数据。这意味着读取操作从系统内存中称为系统文件缓存的区域读取文件数据,而不是从物理磁盘读取文件数据。相应地,写操作将文件数据写入系统文件缓存,而不是写入磁盘,这种类型的缓存称为回写缓存。缓存是按文件对象管理的:更多信息:文件缓存