Constructors and Destructors in C++

In an ideal world, there would never be a chapter dedicated to the performance implications of constructors
and destructors. In that ideal world, constructors and destructors would have no overhead. They would
perform only mandatory initialization and cleanup, and the average compiler would inline them. C code
such as
{
struct X x1;
init(&x1);

cleanup(&x1);
}
would be accomplished in C++ by:
{
X x1;

}
and the cost would be identical. That’s the theory. Down here in the trenches of software development, the
reality is a little different. We often encounter inheritance and composition implementations that are too
flexible and too generic for the problem domain. They may perform computations that are rarely or never
required. In practice, it is not surprising to discover performance overhead associated with inheritance and
composition. This is a limited manifestation of a bigger issue—the fundamental tension between code
reuse and performance. Inheritance and composition involve code reuse. Oftentimes, reusable code will
compute things you don’t really need in a specific scenario. Any time you call functions that do more than
you really need, you will take a performance hit.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • YahooMyWeb
  • Yigg

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)