Avoiding new in fast loops....how?

S

StephQ

Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....

Thank you again!
StephQ
 
P

Puppet_Sock

Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....

You are dealing with a particular kind of optimization.
It's not always going to be the case that the language
supports this in an elegant fashion. Powerfull as C++
is, it's not going to do everything for everybody in
an elegant, efficient manner.

The C++ way of doing this is probably to define a class
and make the function a member function of that class.
Then data members of the class will be the temps. Then
you can simply make one instance of the class per
indepdent use of the function. Add a few functions
to do initialization, copying, etc., and you are on
your way.
Socks
 
I

Ian Collins

StephQ said:
Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.

As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?

Split the allocation problem form the analysis by adding an allocator
for the temporary data. This could start as a simple wrapper for
new/delete and then be tuned to work with its own memory pool to suite
your requirements if new/delete is too slow.

Measure!
 
M

Michael

Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....

Thank you again!
StephQ

Could you reuse the same temp space again and again? In other words,
maybe the best solution is something where you allocate (either on the
stack or the heap) one time outside of the loop, then pass the
'scratch space' to your function. If that would work, it probably
wouldn't matter how you allocate in the first place, since you're only
doing it once. Also, even if it wastes some space, again you're only
doing it once, so it probably wouldn't matter.

Michael
 
P

Piyo

StephQ said:
Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....

Thank you again!
StephQ

You can look into boost::pool and see if that satisfies your
criteria.

http://www.boost.org/libs/pool/doc/index.html

HTH
 
D

David Harmon

This would get my vote; except, I would probably make the function into a member function of that class. Each time the function is called it uses vector::reserve() to
ensure enough memory, which does nothing if there is already enough.
 
S

StephQ

StephQwrote:



Split the allocation problem form the analysis by adding an allocator
for the temporary data. This could start as a simple wrapper for
new/delete and then be tuned to work with its own memory pool to suite
your requirements if new/delete is too slow.
Good tip, thank you!

StephQ
 
S

StephQ

StephQwrote:
Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....
Thank you again!
StephQ

You can look into boost::pool and see if that satisfies your
criteria.

http://www.boost.org/libs/pool/doc/index.html

HTH

I'm having a look, thanks!

StephQ
 
S

StephQ

I decided to just put the temporary vectors inside a class that I'm
already passing (by reference) to the function.
This seems the best compromise to me, as this class is not defined
directly by the user anyway, so I'm avoiding messing with classes that
the user has to deal directly with.
I will look into boost:pool and the wrapper suggestion for more
complex problems.
Back to programming now...

StephQ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top