简体   繁体   English

struct的内存分配(低性能)

[英]Memory allocation for struct (low performance)

I do have a question related to slow performance of allocating memory for several structs. 我确实有一个问题与为几个结构分配内存的性能降低有关。 I have a struct which is looking like: see below 我有一个结构看起来像:见下文

typedef struct _node
{
 // Pointer to leaves & neigbours
 struct _node *children[nrChild], *neighb[nrNeigh];
 // Pointer to parent Node
 struct _node *parentNode;  
 struct _edgeCorner *edgePointID[nrOfEdge];

 int    indexID;                // Value
 double f[latDir];              // Lattice Velos
 double rho;                    // Density
 double  Umag;                  // Mag. velocity    
 int    depth;                  // Depth of octree element

} node

At the beginning of my code I do have to create a lot of them (100.000 – 1.000.000 ) by Using : 在我的代码开头,我必须通过使用创建它们(100.000 - 1.000.000)

tree = new node();

and initiating the elements after it. 并在它之后启动元素。 Unfortunately, this is pretty slow , therefore do anyone of you have an idea to improve the performance? 不幸的是,这很慢 ,所以你们中的任何人都有想法提高性能吗?

Firstly, you'll want to fix it so that it's actually written in C++. 首先,您需要修复它,以便它实际上是用C ++编写的。

struct node
{
    // Pointer to leaves & neigbours
    std::array<std::unique_ptr<node>, nrChild> children;
    std::array<node*, nrNeigh> neighb;
    // Pointer to parent Node
    node* parentNode;  
    std::array<_edgeCorner*, nrOfEdge> edgePointID;

    int    indexID;                // Value
    std::array<double, latDir> f;  // Lattice Velos
    double rho;                    // Density
    double  Umag;                  // Mag. velocity    
    int    depth;                  // Depth of octree element
};

Secondly, in order to improve your performance, you will require a custom allocator. 其次,为了提高性能,您需要一个自定义分配器。 Boost.Pool would be a fine choice- it's a pre-existing solution that is explicitly designed for repeated allocations of the same size, in this case, sizeof(node). Boost.Pool将是一个很好的选择 - 它是一个预先存在的解决方案,明确设计用于相同大小的重复分配,在本例中为sizeof(节点)。 There are other schemes like memory arena that can be even faster, depending on your deallocation needs. 还有其他方案,如内存竞技场,可以更快,取决于您的释放需求。

If you know how many nodes you will have, you could allocate them all in one go: 如果你知道你将拥有多少个节点,你可以一次性分配它们:

node* Nodes = new node[1000000];

You will need to set the values afterwards, just like you would do if you did it one by one. 您需要在之后设置值,就像您逐个进行设置一样。 If it's a lot faster this way, you could try an architecture where you find out how many nodes you will need before allocating them, even if you don't have that number right now. 如果以这种方式更快,你可以尝试一种架构,在分配它们之前你会发现需要多少个节点,即使你现在没有这个数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM