简体   繁体   English

快速的C ++内存分配

[英]C++ Memory allocation in a fast way

I have this code: 我有以下代码:

privateMesh.face[positionSaverFN].vertexMDL = new vector3D[privateMesh.face[positionSaverFN].numOfPoints];

This code is running 67,000 times and it take 0.165sec to do it. 该代码运行67,000次,并且需要0.165秒的时间。 It is too long for me, I am trying to find the fastest way to do it. 对我来说太长了,我正在尝试找到最快的方法。

Any suggestions? 有什么建议么?

Well, if you want to do it 60000 times, there's not much you can do. 好吧,如果您要执行60000次,则无能为力。 Due to the high use of new , it'll be about as fast as it can be. 由于new使用,它会尽可能快地运行。

One way to solve it may be to re-engineer your app so it doesn't have to do it 60000 times. 解决这个问题的方法之一可能是重新设计你的应用程序,所以它并没有这样做60000次。 It may be that you can do it once and just reuse it. 可能您只能执行一次,然后重复使用即可。

Often the fastest way to do something is to not do it :-) 经常做一些事情的最快的方法就是不做 :-)

Calculate total amount of memory needed. 计算所需的内存总量。 Allocate one big buffer. 分配一个大缓冲区。 Access though array of pointers, pointing to consequent regions of this buffer. 通过指针数组访问,指向此缓冲区的后续区域。 Obviously you will need to initialize this array, but it will be much faster then allocating small regions with malloc. 显然,您将需要初始化此数组,但是与使用malloc分配小区域相比,它将更快。

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

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