Avoiding new in fast loops....how?

Discussion in 'C++' started by StephQ, Feb 26, 2007.

  1. StephQ

    StephQ Guest

    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
     
    StephQ, Feb 26, 2007
    #1
    1. Advertisements

  2. StephQ

    Puppet_Sock Guest

    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
     
    Puppet_Sock, Feb 26, 2007
    #2
    1. Advertisements

  3. StephQ

    Ian Collins Guest

    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, 2007
    #3
  4. StephQ

    Michael Guest

    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
     
    Michael, Feb 27, 2007
    #4
  5. StephQ

    Piyo Guest

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

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

    HTH
     
    Piyo, Feb 27, 2007
    #5
  6. StephQ

    David Harmon Guest

    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.
     
    David Harmon, Feb 27, 2007
    #6
  7. StephQ

    StephQ Guest

    Good tip, thank you!

    StephQ
     
    StephQ, Feb 28, 2007
    #7
  8. StephQ

    StephQ Guest

    I'm having a look, thanks!

    StephQ
     
    StephQ, Feb 28, 2007
    #8
  9. StephQ

    StephQ Guest

    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
     
    StephQ, Feb 28, 2007
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.