user2320928
user2320928

Reputation: 309

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?

Upvotes: 3

Views: 3778

Answers (2)

paxdiablo
paxdiablo

Reputation: 881523

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

Upvotes: 5

Leonid Volnitsky
Leonid Volnitsky

Reputation: 9144

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.

Upvotes: 1

Related Questions