结构体的内存分配(低性能)
Memory allocation for struct (low performance)
我确实有一个关于为几个结构分配内存的缓慢性能的问题。我有一个结构体,它看起来像下面的
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
在我的代码的开始,我必须创建很多(100.000 - 1.000.000)使用:
tree = new node();
并初始化它之后的元素。不幸的是,这是相当慢,因此,你们谁有一个想法,以提高性能?
首先,您需要修复它,使其实际上是用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
};
其次,为了提高性能,您将需要一个自定义分配器。提振。Pool将是一个不错的选择——它是一个预先存在的解决方案,明确地为相同大小的重复分配而设计,在本例中为sizeof(node)。还有其他方案,如内存竞技场,可以更快,这取决于您的释放需求。
如果你知道你将有多少节点,你可以一次分配它们:
node* Nodes = new node[1000000];
您需要在之后设置这些值,就像您逐个设置一样。如果这种方式快得多,您可以尝试一种架构,在分配节点之前找出需要多少节点,即使您现在没有这个数量。
相关文章:
- 介于 [固定数组] 和 [带内存分配的指针] 之间的性能
- 为 lambda 分配名称会影响性能吗?
- 解释为什么第二次分配会改变性能
- tcmalloc 与纯堆栈分配性能有多接近
- 使用STD :: MAP在数据及其性能问题中查找重复项.我可以预先分配吗?
- 课堂初始化(分配样式)与构造函数性能
- 比较 C/C++ 中指针分配的性能
- C++ 中的黑白堆分配对象和堆栈分配对象的性能差异
- 内存分配对多线程性能的影响
- 用户定义功能的返回值的分配:性能
- C 的性能建议,以分配内存
- C++特征矩阵运算与内存分配性能
- 更改自动分配内存的范围是否会影响性能
- 链接分配的性能含义是什么
- CUDA内存分配性能
- 为什么预分配的函数指针的性能比分支差
- 两个映射之间的分配-移动语义和性能
- 堆上的分配是否会影响访问性能
- 动态内存分配会降低性能吗
- 如何分析和/或消除由于内存分配引起的性能变化