Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 优化.txt文件创建速度_C++_File_Optimization_Fstream_Ofstream - Fatal编程技术网

C++ 优化.txt文件创建速度

C++ 优化.txt文件创建速度,c++,file,optimization,fstream,ofstream,C++,File,Optimization,Fstream,Ofstream,我已经编写了以下简单的测试代码,它在一个子目录中创建了10000个空的.txt文件 #include <iostream> #include <time.h> #include <string> #include <fstream> void CreateFiles() { int i = 1; while (i <= 10000) { int filename = i; std::strin

我已经编写了以下简单的测试代码,它在一个子目录中创建了10000个空的.txt文件

#include <iostream>
#include <time.h>
#include <string>
#include <fstream>

void CreateFiles()
{
    int i = 1;
    while (i <= 10000) {
        int filename = i;
        std::string string_i = std::to_string(i);
        std::string file_dir = ".\\results\\"+string_i+".txt";
        std::ofstream outfile(file_dir);
        i++;
    }
}

int main()
{
    clock_t tStart1 = clock();
    CreateFiles();
    printf("\nHow long it took to make files: %.2fs\n", (double)(clock() - tStart1)/CLOCKS_PER_SEC);
    std::cin.get();
    return 0;
}
现在一切(创建.txt文件并用短数据填充)都在<代码>~37秒。这是一个巨大的差异。而这仅仅是10000个文件

问题2:这里有什么我可以优化的吗?也许有一些替代方法可以更快地填充.txt文件。或者我忘记了一件非常明显的事情,这会减慢整个过程

或者,也许我有点夸张了,
~37
秒似乎是正常的和优化的


感谢分享您的见解

文件的创建速度取决于硬件,驱动器越快,创建文件的速度就越快

这一点很明显,因为我在ARM处理器上运行了你的代码(Snapdragon 636,在使用termux的移动电话上),现在移动电话的闪存在I/O时速度非常快。所以它大部分时间运行不到3秒,有些时间运行不到5秒。这种变化是预期的,因为驱动器必须处理多进程读写您报告说您的硬件花费了47秒。因此您可以安全地得出结论,I/O速度显著依赖于硬件。


尽管如此,我还是想对代码进行一些优化,我使用了两种不同的方法

  • 对I/O使用C对应项

  • 使用C++,但一次写入块中。

我在手机上进行了模拟。我运行了50次,结果如下

  • C是最快的,使用fprintf在10000个文本文件上写单词平均需要2.73928秒


  • C++一次完成一行代码的编写耗时2.7899秒。我使用sprintf将完整的行转换成一个char[],然后使用编写。文件的创建速度取决于硬件,驱动器越快,创建文件的速度就越快

    这一点很明显,因为我在ARM处理器上运行了你的代码(Snapdragon 636,在使用termux的移动电话上),现在移动电话的闪存在I/O时速度非常快。所以它大部分时间运行不到3秒,有些时间运行不到5秒。这种变化是预期的,因为驱动器必须处理多进程读写您报告说您的硬件花费了47秒。因此您可以安全地得出结论,I/O速度显著依赖于硬件。


    尽管如此,我还是想对代码进行一些优化,我使用了两种不同的方法

    • 对I/O使用C对应项

    • 使用C++,但一次写入块中。

    我在手机上进行了模拟。我运行了50次,结果如下

    • C是最快的,使用fprintf在10000个文本文件上写单词平均需要2.73928秒


    • C++一次完成一行代码的编写耗时2.7899秒。我使用SaaTrFF来将完整的行变成一个字符[],然后用No.No编写,IO流在C++中不是很快。如果您只关注性能,那么应该尝试C语言。看一看,它可以直接处理流。是的,写入文件很昂贵,特别是当你需要高吞吐量的时候。在向文件写入内容时,数量级的差异并不奇怪。我猜这是磁盘能够处理的最快速度。很可能是磁盘本身在减慢速度。在第一个测试中,它只需要更改磁盘上一个位置的目录。在第二种情况下,它必须访问磁盘上的每个文件,才能将信息放在磁盘上。为了让程序更快地创建文件,我可以在这里优化什么吗?--尝试优化任何与文件写入有关的代码时要小心。对于硬件来说,最合适的可能是没有效果或更坏,当在不同的硬件上运行时,事情会变得更慢。在一个目录中有数千个文件也会导致缓慢的查找。没有新的,IO流在C++中不是很快。如果您只关注性能,那么应该尝试C语言。看一看,它可以直接处理流。是的,写入文件很昂贵,特别是当你需要高吞吐量的时候。在向文件写入内容时,数量级的差异并不奇怪。我猜这是磁盘能够处理的最快速度。很可能是磁盘本身在减慢速度。在第一个测试中,它只需要更改磁盘上一个位置的目录。在第二种情况下,它必须访问磁盘上的每个文件,才能将信息放在磁盘上。为了让程序更快地创建文件,我可以在这里优化什么吗?--尝试优化任何与文件写入有关的代码时要小心。对于您的硬件来说,最适合您的可能没有效果或更糟的是,在不同的硬件上运行时会使速度变慢。在一个目录中有数千个文件也会导致查找速度变慢。请注意,这也依赖于编译器,Linux/gcc提供了一个默认的
      IO_BUFSIZ
      (我相信现在已重命名为
      LIO_BUFSIZE
      )当Windows VS仅提供
      512
      时,字节数为
      8192
      字节。因此,如链接答案中所示,“增加”到固定的
      4096
      ,实际上会将Linux上的默认I/O缓冲区大小减少一半。(在测试和报告方面做得很好)注意,这也依赖于编译器,Linux/gcc提供了一个默认的
      IO_BUFSIZ
      (我认为现在重命名为
      LIO_BUFSIZE
      )字节数
      8192
      ,而Windows VS只提供
      512
      。因此,如链接答案中所示,“增加”到固定的
      4096
      ,实际上会将Linux上的默认I/O缓冲区大小减少一半。(在测试和维修方面做得很好
      void CreateFiles()
      {
          int i = 1;
          while (i <= 10000) {
              int filename = i;
              std::string string_i = std::to_string(i);
              std::string file_dir = ".\\results\\"+string_i+".txt";
              std::ofstream outfile(file_dir);
      
              // Here is the part where I am filling the .txt with some data
              outfile << i << " some " << i << " constant " << i << " text " << i << " . . . " 
              << i << " --more text-- " << i << " --even more-- " << i;
              i++;
          }
      }
      
      #include <iostream>
      #include <time.h>
      #include <string>
      #include <fstream>
      #include <stdio.h>
      
      void CreateFiles()
      {
          int i = 1;
          while (i <= 10000) {
             // int filename = i;
              std::string string_i = std::to_string(i);
              std::string file_dir = "./results/"+string_i+".txt";
              std::ofstream outfile(file_dir);
      
              // Here is the part where I am filling the .txt with some data
              outfile << i << " some " << i << " constant " << i << " text " << i << " . . . " 
              << i << " --more text-- " << i << " --even more-- " << i;
              i++;
          }
      }
      
      void CreateFilesOneGo(){
          int i = 1;
          while(i<=10000){
              std::string string_i = std::to_string(i);
              std::string file_dir = "./results3/" + string_i + ".txt";
              char buffer[256];
              sprintf(buffer,"%d some %d constant %d text %d . . . %d --more text-- %d --even more-- %d",i,i,i,i,i,i,i);
              std::ofstream outfile(file_dir);
              outfile << buffer;
              i++;
          }
      }
              
      void CreateFilesFast(){
          int i = 1;
          while(i<=10000){
          // int filename = i;
          std::string string_i = std::to_string(i);
          std::string file_dir = "./results2/"+string_i+".txt";
          FILE *f = fopen(file_dir.c_str(), "w");
          fprintf(f,"%d some %d constant %d text %d . . . %d --more text-- %d --even more-- %d",i,i,i,i,i,i,i);
          fclose(f);
          i++;
          }
      }
      
      int main()
      {
          double normal = 0, one_go = 0, c = 0;
          for (int u=0;u<50;u++){
              std::system("mkdir results results2 results3");
              
              clock_t tStart1 = clock();
              CreateFiles();
              //printf("\nNormal : How long it took to make files: %.2fs\n", (double)(clock() - tStart1)/CLOCKS_PER_SEC);
              normal+=(double)(clock() - tStart1)/CLOCKS_PER_SEC;
             
              tStart1 = clock();
              CreateFilesFast();
              //printf("\nIn C : How long it took to make files: %.2fs\n", (double)(clock() - tStart1)/CLOCKS_PER_SEC);
              c+=(double)(clock() - tStart1)/CLOCKS_PER_SEC;
              
              tStart1 = clock();
              CreateFilesOneGo();
              //printf("\nOne Go : How long it took to make files: %.2fs\n", (double)(clock() - tStart1)/CLOCKS_PER_SEC);
              one_go+=(double)(clock() - tStart1)/CLOCKS_PER_SEC;
              
              std::system("rm -rf results results2 results3");
              std::cout<<"Completed "<<u+1<<"\n";
          }
          
          std::cout<<"C on average took : "<<c/50<<"\n";
          std::cout<<"Normal on average took : "<<normal/50<<"\n";
          std::cout<<"One Go C++ took : "<<one_go/50<<"\n";
          
          return 0;
      }