-
-
Save egtra/1116480 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
| ->> g++4 -Wextra -Wall -DNDEBUG -O3 simple_refcount.cc && ./a.exe | |
| sizeof(String) : 8 | |
| sizeof(std::string) : 4 | |
| intrusive_ptr<String> 1.107 | |
| boost::shared_ptr<string> with make_shared 1.559 | |
| boost::shared_ptr<string> with new 1.778 | |
| std::shared_ptr<string> with make_shared 1.248 | |
| std::shared_ptr<string> with new 1.779 | |
| string 0.655 | |
| ->> cl /W4 /MD /DNDEBUG /O2 simple_refcount.cc && ./a.exe | |
| sizeof(String) : 32 | |
| sizeof(std::string) : 28 | |
| intrusive_ptr<String> 0.317 | |
| boost::shared_ptr<string> with make_shared 0.524 | |
| boost::shared_ptr<string> with new 0.428 | |
| std::shared_ptr<string> with make_shared 0.349 | |
| std::shared_ptr<string> with new 0.448 | |
| string 0.435 |
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 <memory> | |
| #include <vector> | |
| #include <cstdlib> | |
| #include <cstdio> | |
| #include <boost/scoped_ptr.hpp> | |
| #include <boost/shared_ptr.hpp> | |
| #include <boost/make_shared.hpp> | |
| #include <boost/intrusive_ptr.hpp> | |
| #include <boost/timer.hpp> | |
| #include <windows.h> | |
| namespace gfx { | |
| typedef unsigned uint; | |
| template <class T> | |
| class Object { | |
| public: | |
| typedef T object_type; | |
| mutable long refcount_; | |
| Object() : refcount_(0) { | |
| //printf("Object(%p) created\n", this); | |
| } | |
| ~Object() { | |
| //printf("Object(%p) destroyed\n", this); | |
| } | |
| static boost::intrusive_ptr<object_type> create() { | |
| return boost::intrusive_ptr<object_type>(new object_type()); | |
| } | |
| void retain() const { | |
| InterlockedIncrement(&refcount_); | |
| } | |
| void release() const { | |
| if(InterlockedDecrement(&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 boost::intrusive_ptr<String> create(const char* const str) { | |
| return boost::intrusive_ptr<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< boost::intrusive_ptr<String> > v0; | |
| std::vector< boost::intrusive_ptr<String> > v1; | |
| v0.reserve(count); | |
| v1.reserve(count); | |
| for(int i = 0; i < count; i++) { | |
| boost::intrusive_ptr<String> s( String::create(str) ); | |
| v0.push_back(s); | |
| v1.push_back(s); | |
| } | |
| v0.clear(); | |
| v1.clear(); | |
| std::cout << "intrusive_ptr<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(boost::make_shared<std::string>(str) ); | |
| v0.push_back(s); | |
| v1.push_back(s); | |
| } | |
| v0.clear(); | |
| v1.clear(); | |
| std::cout << "boost::shared_ptr<string> with make_shared " << 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 << "boost::shared_ptr<string> with new " << t.elapsed() << std::endl; | |
| } | |
| { | |
| boost::timer t; | |
| std::vector< std::shared_ptr<std::string> > v0; | |
| std::vector< std::shared_ptr<std::string> > v1; | |
| v0.reserve(count); | |
| v1.reserve(count); | |
| for(int i = 0; i < count; i++) { | |
| std::shared_ptr<std::string> s(std::make_shared<std::string>(str) ); | |
| v0.push_back(s); | |
| v1.push_back(s); | |
| } | |
| v0.clear(); | |
| v1.clear(); | |
| std::cout << "std::shared_ptr<string> with make_shared " << t.elapsed() << std::endl; | |
| } | |
| { | |
| boost::timer t; | |
| std::vector< std::shared_ptr<std::string> > v0; | |
| std::vector< std::shared_ptr<std::string> > v1; | |
| v0.reserve(count); | |
| v1.reserve(count); | |
| for(int i = 0; i < count; i++) { | |
| std::shared_ptr<std::string> s( new std::string(str) ); | |
| v0.push_back(s); | |
| v1.push_back(s); | |
| } | |
| v0.clear(); | |
| v1.clear(); | |
| std::cout << "std::shared_ptr<string> with new " << 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