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:
- 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.
- static variables that are constructed once at program startup and then "recycled" with new data when needed.
- some kind of object oriented design pattern I may be unaware of?
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?