Created
July 31, 2011 06:00
-
-
Save gfx/1116465 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ->> clang -Wextra -Wall -lstdc++ -DNDEUG -O3 simple_refcount.cc && ./a.out | |
| sizeof(String) : 8 | |
| sizeof(std::string) : 4 | |
| Handle<String> 0.76 | |
| shared_ptr<string> 1.28 | |
| string 0.63 | |
| ->> g++ -Wextra -Wall -lstdc++ -DNDEUG -O3 simple_refcount.cc && ./a.out | |
| simple_refcount.cc:65: warning: type qualifiers ignored on function return type | |
| sizeof(String) : 8 | |
| sizeof(std::string) : 4 | |
| Handle<String> 0.77 | |
| shared_ptr<string> 1.41 | |
| string 0.65 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <cstdlib> | |
| #include <cstdio> | |
| #include <boost/scoped_ptr.hpp> | |
| #include <boost/shared_ptr.hpp> | |
| #include <boost/intrusive_ptr.hpp> | |
| #include <boost/timer.hpp> | |
| namespace gfx { | |
| typedef unsigned int uint; | |
| template <class T> | |
| class Handle { | |
| public: | |
| typedef T object_type; | |
| Handle(object_type* const p) : p_(p) { | |
| p_->retain(); | |
| } | |
| Handle(const Handle<object_type>& other) | |
| : p_(other.p_) { | |
| p_->retain(); | |
| } | |
| ~Handle() { | |
| p_->release(); | |
| } | |
| private: | |
| object_type* p_; | |
| }; | |
| template <class T> | |
| class Object { | |
| public: | |
| typedef T object_type; | |
| mutable uint refcount_; | |
| Object() : refcount_(0) { | |
| //printf("Object(%p) created\n", this); | |
| } | |
| ~Object() { | |
| //printf("Object(%p) destroyed\n", this); | |
| } | |
| static Handle<object_type> create() { | |
| return Handle<object_type>(new object_type()); | |
| } | |
| void retain() const { | |
| ++refcount_; | |
| } | |
| void release() const { | |
| --refcount_; | |
| if(refcount_ == 0) { | |
| delete this; | |
| } | |
| } | |
| const uint refcount() const { | |
| return refcount_; | |
| } | |
| }; | |
| template <class T> | |
| void intrusive_ptr_add_ref(const Object<T>* const o) { | |
| o->retain(); | |
| } | |
| template <class T> | |
| void intrusive_ptr_release(const Object<T>* const o) { | |
| o->release(); | |
| } | |
| class String : public Object<String> { | |
| public: | |
| std::string str_; | |
| String(const char* const str) : str_(str) { } | |
| static Handle<String> create(const char* const str) { | |
| return Handle<String>(new String(str)); | |
| } | |
| const std::string str() const { return str_; } | |
| }; | |
| } | |
| int main() { | |
| using namespace gfx; | |
| std::cout << "sizeof(String) : " << sizeof(String) << std::endl; | |
| std::cout << "sizeof(std::string) : " << sizeof(std::string) << std::endl; | |
| int const count = 100 * 10000; | |
| const char* const str = "foobarbaz foobarbaz foobarbaz"; | |
| { | |
| boost::timer t; | |
| std::vector< Handle<String> > v0; | |
| std::vector< Handle<String> > v1; | |
| v0.reserve(count); | |
| v1.reserve(count); | |
| for(int i = 0; i < count; i++) { | |
| Handle<String> s( String::create(str) ); | |
| v0.push_back(s); | |
| v1.push_back(s); | |
| } | |
| v0.clear(); | |
| v1.clear(); | |
| std::cout << "Handle<String> " << t.elapsed() << std::endl; | |
| } | |
| { | |
| boost::timer t; | |
| std::vector< boost::shared_ptr<std::string> > v0; | |
| std::vector< boost::shared_ptr<std::string> > v1; | |
| v0.reserve(count); | |
| v1.reserve(count); | |
| for(int i = 0; i < count; i++) { | |
| boost::shared_ptr<std::string> s( new std::string(str) ); | |
| v0.push_back(s); | |
| v1.push_back(s); | |
| } | |
| v0.clear(); | |
| v1.clear(); | |
| std::cout << "shared_ptr<string> " << t.elapsed() << std::endl; | |
| } | |
| { | |
| boost::timer t; | |
| std::vector< std::string > v0; | |
| std::vector< std::string > v1; | |
| v0.reserve(count); | |
| v1.reserve(count); | |
| for(int i = 0; i < count; i++) { | |
| std::string s( str ); | |
| v0.push_back(s); | |
| v1.push_back(s); | |
| } | |
| v0.clear(); | |
| v1.clear(); | |
| std::cout << "string " << t.elapsed() << std::endl; | |
| } | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment