syncjilo.blogg.se

Unique benchmark testing
Unique benchmark testing







unique benchmark testing
  1. UNIQUE BENCHMARK TESTING PC
  2. UNIQUE BENCHMARK TESTING WINDOWS

Even std::make_shared has a performance overhead of about 10%. Admittedly, the std::shared_ptr is about two times slower than new and delete. My conclusion to std::shared_ptr is not so easy.That is great because std::unique_ptr offers an excellent benefit by automatically managing the lifetime of its resource without any additional cost. std::unique_ptr has no memory or performance overhead compared to the explicit usage of new and delete.std::shared_ptr is about two times slower than new and deletes even with optimization. You should not use std::shared_ptr and std::make_shared without optimization.std::unique_ptr, std::make_unique, and with minor deviations, std::make_shared are in the same performance range as new and delete.Interestingly, this observation will not hold for new and delete. The optimized program is about 2 to 3 times faster. But these observations also hold for the other smart pointers. In the case of std::make_shared_ptr, the program with maximum optimizations is almost ten times faster. I want to draw a few interesting conclusions from the table.

unique benchmark testing

UNIQUE BENCHMARK TESTING WINDOWS

I’m not comparing Windows and Linux.įor simplicity reasons, I will not show the screenshots of the programs and present you only the table holding the numbers. Therefore, I’m interested in comparing the raw memory allocation and the smart pointers.

UNIQUE BENCHMARK TESTING PC

I must admit that my Windows PC is less powerful than my Linux PC. Therefore, I can run my performance test with maximum and without optimization. Although cl.exe officially supports only C++11, the helper function std::make_unique is already available. The cl.exe is part of Microsoft Visual Studio 2015. I use GCC 4.9.2 and a cl.exe for my performance tests. std::make_unique is available since C++14 the other smart pointer functionality since C++11. std::make_shared make one memory allocation out of them. Memory is necessary for the managed resource and the reference counters. There are more memory allocations necessary for the creation of a std::shared_ptr. In particular, std::make_shared is very interesting. They create the smart pointer, respectively. The functions std::make_shared (line 16) and std::make_unique(line 18) are quite handy. In this small program, handling the smart pointer (lines 15 – 18) is much simpler because it automatically releases its dynamically created int variable if it goes out of scope. I compare in my test the explicit calls of new and delete (lines 13 and 14) with the usage of std::shared_ptr (line 15), std::make_shared (line 16), std::unique_ptr (line 17), and std::make_unique (line 18). Std::cout << "time native: " << dur.count() << " seconds" << std::endl Std::chrono::duration dur= std::chrono::system_clock::now() - start all.cpp #include #include static const long long numInt= 100000000 Īuto start = std::chrono::system_clock::now() įor ( long long i=0 i tmp(new int(i)) // std::shared_ptr tmp(std::make_shared(i)) // std::unique_ptr tmp(new int(i)) // std::unique_ptr tmp(std::make_unique(i)) That’s the overhead a std::shared_ptr has in opposite to a raw pointer. (To be precise, there is an additional reference counter for the std::weak_ptr). Therefore, the std::shared_ptr needs additional memory for the reference counter. The reference count will be decreased if the std::shared_ptr goes out of scope. That means if a std::shared_ptr is copied, the reference counter will be increased. In opposition to the std::unique_ptr, the std::shared_ptr has a little overhead. As I mentioned, that is the special use case. If this deleter function has been stated, you will have an enriched std::unique_ptr and pay for it.

unique benchmark testing

But what does default mean? You can parametrize a std::unique_ptr with a special deleter function. That means std::unique_ptr is as big as its underlying raw pointer. Std::unique_ptr needs, by default, no additional memory. I will state the result of my tests before I show you the raw numbers: There are only a few reasons in modern C++ justifying the memory management with new and delete. My first candidate, std::unique_ptr takes care of the lifetime of one resource exclusively std::shared_ptr shares the ownership of a resource with another std::shared_ptr. I will have a closer look in this post regarding memory and performance overhead on two of them. C++11 offers four different smart pointers.









Unique benchmark testing