相关疑难解决方法(0)

'real','user'和'sys'在time(1)的输出中意味着什么?

$ time foo
real        0m0.003s
user        0m0.000s
sys         0m0.004s
$
Run Code Online (Sandbox Code Playgroud)

"真实","用户"和"系统"在时间输出中意味着什么?

在对我的应用进行基准测试时哪一个有意义?

unix time benchmarking

1622
推荐指数
6
解决办法
42万
查看次数

用C++编写二进制文件的速度非常快

我正在尝试将大量数据写入我的SSD(固态硬盘).大量的我的意思是80GB.

我浏览网页寻求解决方案,但我想出的最好的是:

#include <fstream>
const unsigned long long size = 64ULL*1024ULL*1024ULL;
unsigned long long a[size];
int main()
{
    std::fstream myfile;
    myfile = std::fstream("file.binary", std::ios::out | std::ios::binary);
    //Here would be some error handling
    for(int i = 0; i < 32; ++i){
        //Some calculations to fill a[]
        myfile.write((char*)&a,size*sizeof(unsigned long long));
    }
    myfile.close();
}
Run Code Online (Sandbox Code Playgroud)

使用Visual Studio 2010进行编译并完全优化并在Windows7下运行,此程序最大可达20MB/s.让我感到困扰的是,Windows可以将文件从其他SSD复制到此SSD,速度介于150MB/s和200MB/s之间.所以至少快7倍.这就是为什么我认为我应该能够更快.

我有什么想法可以加快我的写作速度?

c++ io optimization performance file-io

221
推荐指数
8
解决办法
17万
查看次数

Qt - 将文件从一个目录复制到另一个目录

我正在使用QT,我无法找到如何将文件从一个目录复制到另一个目录?我怎样才能做到这一点?

c++ qt

29
推荐指数
2
解决办法
4万
查看次数

C++ FileIO Copy -VS-系统("cp file1.x file2.x)

编写文件复制例程是否更快/更高效,还是应该只执行对cp的系统调用?

(文件系统可能有所不同[nfs,local,reiser等],但它总是在CentOS linux系统上)

c++ linux filesystems g++

12
推荐指数
1
解决办法
2万
查看次数

为什么用C语言复制文件比C++快得多?

我已经在一个相当大的C++项目上工作了几个星期了.我最初的目标是使用这个项目来学习C++ 11并仅使用纯C++代码并避免手动分配和C构造.但是,我认为这个问题会迫使我用C来做一个小函数,我想知道为什么.

基本上我有一个保存功能,它会在我更改其中的数据之前将一个稍大的二进制文件复制到一个单独的位置.文件本身是CD图像,最大大小约为700MB.这是我使用的原始C++代码:

std::ios::sync_with_stdio(false);

std::ifstream in(infile, std::ios::binary);
std::ofstream out(outfile, std::ios::binary);

std::copy(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(out));

out.close();
in.close();
Run Code Online (Sandbox Code Playgroud)

与690MB文件一起使用时,此代码只需不到4分钟即可完成.我用多个文件运行它,它总是相同的结果; 没有3分钟.但是,我还发现以下方式运行得更快一点,但仍然没有C那么快:

std::ios::sync_with_stdio(false);

std::ifstream in(infile, std::ios::binary);
std::ofstream out(outfile, std::ios::binary);

out << in.rdbuf();

out.close();
in.close();
Run Code Online (Sandbox Code Playgroud)

这个花了24秒,但它仍然比C慢约20倍.

环顾四周之后,我发现有人需要写一个80GB的文件并看到他可以使用C全速写入.我决定尝试使用这段代码:

FILE *in = fopen(infile, "rb");
FILE *out = fopen(outfile, "wb");

char buf[1024];
int read = 0;

//  Read data in 1kb chunks and write to output file
while ((read = fread(buf, 1, 1024, in)) == 1024)
{
  fwrite(buf, 1, 1024, out);
}

//  If there is any data left …
Run Code Online (Sandbox Code Playgroud)

c c++ performance file-io

12
推荐指数
1
解决办法
2522
查看次数

c ++标准中的重载vs默认参数

我正在读另一个问题,它让我思考.通常,标准指定在其描述中具有默认参数的函数.标准是否允许将这些作为重载写入?

例如,标准说std::basic_string::copy具有以下声明:

size_type copy(Ch* p, size_type n, size_type pos = 0) const;
Run Code Online (Sandbox Code Playgroud)

标准库的符合实现可以实现这个,而不是像这样的两个函数吗?

size_type copy(Ch* p, size_type n, size_type pos) const;
size_type copy(Ch* p, size_type n) const;
Run Code Online (Sandbox Code Playgroud)

在此示例中,第二个版本可以跳过if(pos > size()) { throw out_of_range(); }第一个版本中必需的测试.微观优化,但你仍然看到了例子的重点.

c++ overloading std

11
推荐指数
1
解决办法
1649
查看次数

在Linux上用c ++移动文件的更快捷方式

我正在尝试使用C++在linux上移动文件.问题是,源文件和目标文件夹可以位于不同的分区中.所以我不能简单地移动文件.好.我决定复制该文件并删除旧文件.

//-----
bool copyFile(string source, string destination)
{
    bool retval = false;
    ifstream srcF (source.c_str(), fstream::binary);
    ofstream destF (destination.c_str(), fstream::trunc|fstream::binary);
    if(srcF.is_open() && destF.is_open()){
        destF << srcF.rdbuf(); //copy files binary stream
        retval = true;
    }
    srcF.close();
    destF.close();
    return retval;
}
//-----
Run Code Online (Sandbox Code Playgroud)

现在我的问题.我意识到,这种方法很慢.100MB需要47秒.只需使用console命令复制文件需要2-3秒.

有人有想法吗?

c++ linux filesystems file-io partition

5
推荐指数
1
解决办法
1844
查看次数

sendfile64只复制约2GB

我需要使用sendfile64来复制大约16GB的文件.到目前为止我取得的成就是

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/sendfile.h>
#include <sys/stat.h>

int main (int argc, char** argv)
{
  long long src;
  long long dest;
  struct stat64 stat_buf;
  off64_t offset = 0LL;
  long long rc;

  if (argc != 3) {
    fprintf(stderr, "usage: %s <source> <destination>\n", argv[0]);
    exit(1);
  }

  src = open64(argv[1], O_RDONLY);
  if (src == -1) {
    fprintf(stderr, "unable to open '%s': %s\n", argv[1], strerror(errno));
    exit(1);
  }

  fstat64(src, &stat_buf);

  dest = open64(argv[2], O_WRONLY|O_CREAT, stat_buf.st_mode);
  if …
Run Code Online (Sandbox Code Playgroud)

c c++ linux sendfile

5
推荐指数
1
解决办法
1076
查看次数

使用/不使用ios :: binary模式打开流时使用读/写的区别

在我使用以下代码片段的实验中,我没有找到任何特别的区别,无论我是否使用ios创建流:二进制模式:

int main()
{
    ifstream ostr("Main.cpp", ios::in | ios::binary | ios::ate);
    if (ostr.is_open())
    {
        int size = ostr.tellg();
        char * memBlock = new char[size + 1];
        ostr.seekg(0, ios::beg);
        ostr.read(memBlock, size);
        memBlock[size] = '\0';
        ofstream file("trip.cpp", ios::out | ios::binary);
        file.write(memBlock, size);
        ostr.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我试图将原始源文件复制到另一个具有不同名称的文件中.

我的问题是当使用/不使用ios :: binary模式打开fstream对象时,读/写调用(与二进制文件IO相关)之间的区别是什么?使用二进制模式有什么好处吗?什么时候何时何时不使用它来做文件IO?

c++ io binaryfiles filestream

4
推荐指数
1
解决办法
1723
查看次数

如何将文本文件复制到另一个文件中?

如何将文本文件复制到另一个文件中?我试过这个:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream infile("input.txt");
    ofstream outfile("output.txt");
    outfile << infile;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这最终会留下以下值output.txt:0x28fe78.

我究竟做错了什么?

c++ file stream

3
推荐指数
1
解决办法
2万
查看次数