0

A couple of months ago I wrote a C++ program for computational mathematics that was supposed to compete with a highly optimized C code.

After a while I did manage to get it fast enough to beat the C code, but before that happened while analyzing what was slow, I was really surprised about how long time it can take for C++ to create objects, especially if we have complicated class structures and templates.

I realized the usefulness to be able to "lazify" or postpone object creation if it is unnecessary. Do there exist any good methods to do this?

Some things I thought about, but don't have any sources on recommended ways to do:

  1. hash-tables, storing already created objects in case we are likely to want to re-solve same equation systems thousands of times and only sporadically create a new one.
  2. static variables that are constructed once at program startup and then "recycled" with new data when needed.
  3. some kind of object oriented design pattern I may be unaware of?
  • 2
    I often hear what you describe in point 1 as either caching or memoization. Point 2 sounds like pooling. So it seems you're on the right track! – user1118321 Aug 3 at 15:03
  • I was really surprised about how long time it can take for C++ to create objects, especially if we have complicated class structures and templates. What exactly was taking that much time? Where you perhaps allocating heap memory in a hot loop? – D. Jurcau Aug 4 at 12:31
  • @D.Jurcau I mean it took lots of nanoseconds every time. Nanoseconds which could be spent doing vectorized calculations instead. I once had some creation of vector objects inside a conjugate gradient loop instead of creating them before starting the iterations and then reusing them. – mathreadler Aug 4 at 13:00
  • Perhaps you can seek help from a friend who can read disassembly code and help identify sources of latency which you may have overlooked. As far as performance is concerned, the choice of programming language matters. – rwong Aug 4 at 18:45
  • I have coded my share of assembler language so I can do that myself. I just wanted some c++ input. :) – mathreadler Aug 4 at 19:17
2

I’d look into the Object Pool pattern that basically does the recycling you mention yourself.

It’s actually how both Android and iOS handle the problem of “staggering” while scrolling on a list of items e.g. a newsfeed or product list.

Close relatives are the Prototype and Flyweight patterns.

Among these three and some sort of factory and depending on the specifics of your use case, you should be able to drop the amount of instantiations low enough to see performance benefits.

  • Wow really good thanks. – mathreadler Aug 4 at 13:04
1

As long as no heap memory is allocated, creating objects in C++ (when no calculations are performed in the constructor) isn't more expensive than calling a function and assigning individual variables, as can be seen in the following compilation result: https://godbolt.org/z/n016Fw

If the constructor performs mathematical computations then it depends on the problem at hand and it's time/space constraints if caching/memoization make sense.

Please remember to always enable optimizations when benchmarking C++ code because inlining oportunities and other types of compiler optimizations can make a huge difference in the result.

  • LOL. I am not allocating 3 ints. Slightly more complicated classes. – mathreadler Aug 5 at 6:37
  • 2
    @mathreadler there's unlikely to be a difference in initialising N ints and assigning N ints, to distinguish creating a Foo vs re-using a Foo. – Caleth Aug 5 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.