Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 232
user2320928 Avatar asked Nov 19 '25 18:11

user2320928


2 Answers

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 :-)

like image 169
paxdiablo Avatar answered Nov 21 '25 07:11

paxdiablo


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.

like image 30
Leonid Volnitsky Avatar answered Nov 21 '25 08:11

Leonid Volnitsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!