小编Dom*_*fer的帖子

用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万
查看次数

将带孔的多边形转换为多个没有孔的简单多边形

我正在处理IfcFace.我给了一个带孔的简单多边形,我需要将它转换成多个没有孔的简单多边形,以便CAD进一步处理它.一点点演示ilustration:

在此输入图像描述

我最好的方法是做一个约束的delaunay三角剖分并将三角形重新加入更大的多边形.像这样: 在此输入图像描述 但是由于浮点精度和算法不稳定性,delaunay三角剖分甚至更多的约束部分往往因难以输入而失败.我的输入有时会生成高度为1e-8且基本长度为1的三角形.

是否有更好的更强大的算法来实现这种转换?

polygons

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

将#ifdef转换为C++中的模板元程序

我在C++类中有以下代码:

class Features
{
    #define Feature_Size_A 12345
    #define Feature_Size_B 45678
    #define Feature_Size_C 78901
    //#define Feature_Size_D 14725

    const int Feature_Sum = 0
    #ifdef Feature_Size_A
        + Feature_Size_A
    #endif
    #ifdef Feature_Size_B
        + Feature_Size_B
    #endif
    #ifdef Feature_Size_C
        + Feature_Size_C
    #endif
    #ifdef Feature_Size_D
        + Feature_Size_D
    #endif
        ;

    #ifdef Feature_Size_A
        static float Feature_A[Feature_Size_A];
    #endif
    #ifdef Feature_Size_B
        static float Feature_B[Feature_Size_B];
    #endif
    #ifdef Feature_Size_C
        static float Feature_C[Feature_Size_C];
    #endif
    #ifdef Feature_Size_D
        static float Feature_D[Feature_Size_D];
    #endif
};
Run Code Online (Sandbox Code Playgroud)

我过去常常注释掉第4行的功能来编译和运行不同的测试.但是现在我想将类作为模板,因此我可以在同一个程序中实例化具有不同功能的多个版本.

我在考虑这样的事情:

template <bool Feature_A, bool Feature_B, bool Feature_C, bool Feature_D>
class Features …
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming

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

在我自己的类上使用std :: inner_product

我想出了一个名为TinyVector的小班.现在我正在尝试使用std :: inner_product.但我不能让它工作,我不明白为什么这不起作用.我正在使用Visual Studio 2012.

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

template<class T, int N>
class TinyVector {
public:
    TinyVector() { data = vector<T>(N, 0); }
    explicit TinyVector(const T initVal) { data = vector<T>(N, initVal); }
    ~TinyVector() { data.clear(); }
    inline T& operator[](int i) { return data[i]; }
    inline T operator[](int i) const { return data[i]; }
    inline vector<T>::const_iterator begin() const { return data.begin(); } //Line 15
    inline vector<T>::const_iterator end() const { return data.end(); } //Line 16 …
Run Code Online (Sandbox Code Playgroud)

c++ iterator vector

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