简体   繁体   中英

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. 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. Due to the high use of new , it'll be about as fast as it can be.

One way to solve it may be to re-engineer your app so it doesn't have to do it 60000 times. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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