Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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++ 无法足够快地写入位集_C++_Performance_Bitset - Fatal编程技术网

C++ 无法足够快地写入位集

C++ 无法足够快地写入位集,c++,performance,bitset,C++,Performance,Bitset,我需要能够以大约1毫秒/10000比特集的速度写下12比特集。基本上,我提供了12位包(在本例中是位集)中的数据,我需要能够在非常短的时间内存储它们(我选择将它们写入一个文件,如果存在其他方法,可以接受建议) 现在,我已经建立了一个大小为10000的位集数组的示例(以模拟实际得到的结果),并将它们全部写入一个文件中 int main() { std::bitset<12> map[10000]; std::ofstream os("myfile.txt", std

我需要能够以大约1毫秒/10000比特集的速度写下12比特集。基本上,我提供了12位包(在本例中是位集)中的数据,我需要能够在非常短的时间内存储它们(我选择将它们写入一个文件,如果存在其他方法,可以接受建议)

现在,我已经建立了一个大小为10000的位集数组的示例(以模拟实际得到的结果),并将它们全部写入一个文件中

int main()
{

    std::bitset<12> map[10000];

    std::ofstream os("myfile.txt", std::ofstream::binary);
    //From here
    for (int i = 0; i < 10000; ++i)
    {
        os << map[i];
    }
    //to here takes slightly under 7 ms -- too slow
}
intmain()
{
std::位集映射[10000];
std::ofstream操作系统(“myfile.txt”,std::ofstream::binary);
//从这里
对于(int i=0;i<10000;++i)
{
os两项建议:

  • 尽可能减少对操作系统的访问。一次写入多个字节
  • 在写入之前打包位。您当前的解决方案将位作为字符写入,即每一位一个字节。以二进制模式写入,这样会更紧凑(也更快)8倍
#包括
#包括
#包括
int main()
{
std::位集映射[10000];
//使用演示值初始化
对于(int i=0;i<10000;++i){
map[i]=i+1;
}
//将位打包到二进制缓冲区中
标准:向量缓冲区((10000*12+7)/8);
对于(int i=0,j=0,rem=0;i<10000;+i){
无符号长b=map[i]。到_ulong();
缓冲区[j++]|=静态(b>>(4+rem));
缓冲区[j]|=静态_转换(b=8){
rem-=8;
j++;
}
}
//一次写入缓冲区
std::ofstream操作系统(“myfile.bin”,std::ofstream::binary);
write(reinterpret_cast(buffer.data()),buffer.size());
os.close();//不要忘记关闭()以刷新文件
}
如果希望保留文本文件格式,请至少启用缓冲:

int main()
{
    std::bitset<12> map[10000];

    // Enable buffering
    std::vector<char> buf(256 * 1024);
    std::ofstream os("myfile.txt", std::ofstream::binary);
    os.rdbuf()->pubsetbuf(buf.data(), buf.size());

    for (int i = 0; i < 10000; ++i)
    {
        os << map[i];
    }
    os.close(); // to flush the buffer
}
intmain()
{
std::位集映射[10000];
//启用缓冲
标准:向量buf(256*1024);
std::ofstream操作系统(“myfile.txt”,std::ofstream::binary);
os.rdbuf()->pubstebuf(buf.data(),buf.size());
对于(int i=0;i<10000;++i)
{

Os如何把它放在一个缓冲区中,做一个代码>写< /代码>,让操作系统和硬件处理其余的部分?你真的不想在关键路径中使用C++流。我建议使用内存映射文件,它们可能提供最好的性能。如果你可以改变文件格式,你可以轻松地写8X少D。因为输出似乎只写一次,而不是读回(至少在代码的这一部分),只需执行
write()
可能足够了,不需要内存映射文件。不过我同意@TEDLYNMO的观点,即使在流中内置了缓冲,一次写入也比10000次单独写入快很多。您应该发布用于构建程序的编译器选项。如果您正在对未优化或“调试”进行计时构建时,您显示给我们的计时毫无意义。我会尝试将
std::bitset map[10000];
映射到
uint16\u t缓冲区[10000]
看看这是否足够快。从大大简化的代码中获得的长期收益在未来较低的O&M中可能值得一点性能影响。我不寒而栗地想,如果
std::bitset
在几年内需要增长到
std::bitset
,并且如果您第一次这样做的话,工作将交给新的程序员吞咽是唯一足够快的。
int main()
{
    std::bitset<12> map[10000];

    // Enable buffering
    std::vector<char> buf(256 * 1024);
    std::ofstream os("myfile.txt", std::ofstream::binary);
    os.rdbuf()->pubsetbuf(buf.data(), buf.size());

    for (int i = 0; i < 10000; ++i)
    {
        os << map[i];
    }
    os.close(); // to flush the buffer
}