c++ - What's the cost of using private class data pattern? -


i used have timeunit declaration in library:

solution1:

typedef boost::posix::ptime timeunit;  timeunit createtimeunit( int hours, int minutes ); std::string tostring( const timeunit& timeunit ); 

let's i'd move more object-oriented:

solution2:

class timeunit : public boost::posix::ptime { public:     timeunit( int hours, int minutes );      std::string tostring() const; }; 

and now, let's don't want libraries using class directly depend on boost, i'd use private class data pattern, remove boost reference header file:

solution3:

class timeunitprivate; class timeunit { public:     timeunit( int hours, int minutes );     ~timeunit();      std::string tostring() const; public:     timeunitprivate* m_data; }; 

timeunitprivate being same solution2's timeunit , new timeunit being simply:

timeunit::timeunit( int hours, int minutes ) :      m_data( hours, minutes ) {}  timeunit::~timeunit() {     delete m_data; }  std::string timeunit::tostring() {     return m_data->tostring(); } 

solution3 smart , i'd save compilation time + limit boost dependencies sure.

but wondering cost of each solution in term of:

  • memory usage. 3 solutions of timeunit objects need same amount of bytes stored in memory? if not, 1 best (i suppose it's solution1)
  • performance: sure, solution3's tostring() slower solution2's (as function cannot inline, there additional function call needed in end). that's not big deal. i'm wondering if object creation/destruction slower...how 3 solutions sorted in term of performance object creation/destruction?

the first 2 solutions identical in terms of memory usage , performance. 1 more object-y other, purely matter of preference.

the handle-body/pimpl idiom solution take more memory (by size of pointer) , slower (an indirection each call, allocation on creation). how slower? you'll have time it. memory usage , performance hit hurt in comparison speedup in compilation time , reduction in dependency? purely dependent on whatever project doing. applications, performance on scale doesn't matter in slightest it's worth speeding compilation. other applications, complete non-starter. there's no universal answer.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -