performance - How much is the overhead of smart pointers compared to normal pointers in C++? -
how overhead of smart pointers compared normal pointers in c++11? in other words, code going slower if use smart pointers, , if so, how slower?
specifically, i'm asking c++11 std::shared_ptr
, std::unique_ptr
.
obviously, stuff pushed down stack going larger (at least think so), because smart pointer needs store internal state (reference count, etc), question is, how going affect performance, if @ all?
for example, return smart pointer function instead of normal pointer:
std::shared_ptr<const value> getvalue(); // versus const value *getvalue();
or, example, when 1 of functions accept smart pointer parameter instead of normal pointer:
void setvalue(std::shared_ptr<const value> val); // versus void setvalue(const value *val);
std::unique_ptr
has memory overhead if provide non-trivial deleter.
std::shared_ptr
has memory overhead reference counter, though small.
std::unique_ptr
has time overhead during constructor (if has copy provided deleter and/or null-initialize pointer) , during destructor (to destroy owned object).
std::shared_ptr
has time overhead in constructor (to create reference counter), in destructor (to decrement reference counter , possibly destroy object) , in assignment operator (to increment reference counter). due thread-safety guarantees of std::shared_ptr
, these increments/decrements atomic, adding more overhead.
note none of them has time overhead in dereferencing (in getting reference owned object), while operation seems common pointers.
to sum up, there overhead, shouldn't make code slow unless continuously create , destroy smart pointers.
Comments
Post a Comment