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