diff options
author | Sven Gothel <[email protected]> | 2021-10-23 09:54:42 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-10-23 09:54:42 +0200 |
commit | c96ec573c889848af93fa04dea466369f82ab7b8 (patch) | |
tree | a86273ff3aa71d776a88b75575f498af60642f96 | |
parent | 239a9866e7e23a5c9cc0ee156d6b487580c9874f (diff) |
Add perf_ptr01: Perf test on pointer ops (native, unique and shared)
-rw-r--r-- | Makefile | 15 | ||||
-rw-r--r-- | src/perf_ptr01.cpp | 83 |
2 files changed, 94 insertions, 4 deletions
@@ -1,8 +1,13 @@ -CXX = c++ -x c++ -std=c++0x -c -LN = c++ +CXXEXE = c++ +#CXXEXE = clang++ -CXXOPTS = -g -O3 -Wall -Isrc -Iinclude +CXX = $(CXXEXE) -x c++ -std=c++2a -c +CXXLN = $(CXXEXE) -x c++ -std=c++2a +LN = $(CXXEXE) + +#CXXOPTS = -g -O3 -Wall -Isrc -Iinclude +CXXOPTS = -O3 -Wall -Isrc -Iinclude # .SUFFIXES : .cpp .o @@ -12,10 +17,12 @@ obj/%.o: src/%.cpp bin/% : obj/%.o $(LN) -o $@ $(CXXOPTS) $< -EXES = bin/rvalue_01 bin/string_01 bin/string_02 +EXES = bin/rvalue_01 bin/string_01 bin/string_02 bin/perf_ptr01 all: obj bin $(EXES) +#bin/perf_ptr01_s: src/perf_ptr01.cpp +# $(CXXLN) -o $@ -static-libstdc++ $(CCOPTS) $< obj: mkdir $@ diff --git a/src/perf_ptr01.cpp b/src/perf_ptr01.cpp new file mode 100644 index 0000000..d01f692 --- /dev/null +++ b/src/perf_ptr01.cpp @@ -0,0 +1,83 @@ +#include <chrono> +#include <iostream> +#include <memory> + +static const size_t numInt= 100000000; // 100M + +void perf_test(const int mode, const std::string & modeName) { + std::chrono::time_point<std::chrono::system_clock> t0 = std::chrono::system_clock::now(); + std::chrono::time_point<std::chrono::system_clock> t1; + + switch(mode) { + case 0: { + for ( size_t i=0 ; i < numInt; ++i) { + int* tmp(new int(i)); + delete tmp; + } + t1 = std::chrono::system_clock::now(); + int* tmp(new int(0)); + for ( size_t i=0 ; i < numInt; ++i) { + *tmp = 1; + } + delete tmp; + } break; + + case 10: { + for ( size_t i=0 ; i < numInt; ++i) { + std::unique_ptr<int> tmp(new int(i)); + } + t1 = std::chrono::system_clock::now(); + std::unique_ptr<int> tmp(new int(0)); + for ( size_t i=0 ; i < numInt; ++i) { + *tmp = 1; + } + } break; + + case 11: { + for ( size_t i=0 ; i < numInt; ++i) { + std::unique_ptr<int> tmp(std::make_unique<int>(i)); + } + t1 = std::chrono::system_clock::now(); + std::unique_ptr<int> tmp(std::make_unique<int>(0)); + for ( size_t i=0 ; i < numInt; ++i) { + *tmp = 1; + } + } break; + + case 20: { + for ( size_t i=0 ; i < numInt; ++i) { + std::shared_ptr<int> tmp(new int(i)); + } + t1 = std::chrono::system_clock::now(); + std::shared_ptr<int> tmp(new int(0)); + for ( size_t i=0 ; i < numInt; ++i) { + *tmp = 1; + } + } break; + + case 21: { + for ( size_t i=0 ; i < numInt; ++i) { + std::shared_ptr<int> tmp(std::make_shared<int>(i)); + } + t1 = std::chrono::system_clock::now(); + std::shared_ptr<int> tmp(std::make_shared<int>(0)); + for ( size_t i=0 ; i < numInt; ++i) { + *tmp = 1; + } + } break; + } + std::chrono::time_point<std::chrono::system_clock> t2 = std::chrono::system_clock::now(); + std::chrono::duration<double> dur1 = t1 - t0; + std::chrono::duration<double> dur2 = t2 - t1; + std::chrono::duration<double> durT = t2 - t0; + std::cout << "time[s] " << modeName << ": ctor/del " << dur1.count() << ", assign " << dur2.count() << ", total " << durT.count() << std::endl; +} + +int main() { + perf_test( 0, "native-new "); + perf_test(10, "unique-new "); + perf_test(11, "unique-make"); + perf_test(20, "shared-new "); + perf_test(21, "shared-make"); +} + |