424,667 Members | 2,603 Online
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

# Avoiding new in fast loops....how?

 P: n/a 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 Feb 26 '07 #1
8 Replies

 P: n/a On Feb 26, 6:18 pm, "StephQ"

 P: n/a StephQ wrote: 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! -- Ian Collins. Feb 26 '07 #3

 P: n/a On Feb 26, 3:18 pm, "StephQ"

 P: n/a StephQ wrote: 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 Feb 27 '07 #5

 P: n/a On 26 Feb 2007 15:42:13 -0800 in comp.lang.c++, "Puppet_Sock" 4) define a class for storage purposes (with the 4 vectors) and add tothe function's arguments.... 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. Feb 27 '07 #6

 P: n/a On Feb 26, 11:48 pm, Ian Collins 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. Good tip, thank you! StephQ Feb 28 '07 #7

 P: n/a On Feb 27, 12:41 am, Piyo

 P: n/a 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 Feb 28 '07 #9

### This discussion thread is closed

Replies have been disabled for this discussion.