1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <benchmark/benchmark.h> 17 #include <deque> 18 19 #include <meta/api/object.h> 20 #include <meta/base/namespace.h> 21 22 #include "utils.h" 23 24 META_BEGIN_NAMESPACE() 25 namespace benchmarks { 26 SharedPtrConstruct(benchmark::State & state)27void SharedPtrConstruct(benchmark::State& state) 28 { 29 auto object = CreateBenchmarkType(); 30 for (auto _ : state) { 31 BASE_NS::shared_ptr<IBenchmarkType> p(object.get()); 32 benchmark::DoNotOptimize(p); 33 } 34 } 35 SharedPtrCopy(benchmark::State & state)36void SharedPtrCopy(benchmark::State& state) 37 { 38 auto object = CreateBenchmarkType(); 39 for (auto _ : state) { 40 BASE_NS::shared_ptr<IBenchmarkType> p(object); 41 benchmark::DoNotOptimize(p); 42 } 43 } 44 SharedPtrMove(benchmark::State & state)45void SharedPtrMove(benchmark::State& state) 46 { 47 auto object = CreateBenchmarkType(); 48 for (auto _ : state) { 49 BASE_NS::shared_ptr<IBenchmarkType> p(BASE_NS::move(object)); 50 object = BASE_NS::move(p); 51 benchmark::DoNotOptimize(p); 52 } 53 } 54 WeakPtrConstruct(benchmark::State & state)55void WeakPtrConstruct(benchmark::State& state) 56 { 57 auto object = CreateBenchmarkType(); 58 for (auto _ : state) { 59 BASE_NS::weak_ptr<IBenchmarkType> p(object); 60 benchmark::DoNotOptimize(p); 61 } 62 } 63 WeakPtrLock(benchmark::State & state)64void WeakPtrLock(benchmark::State& state) 65 { 66 auto object = CreateBenchmarkType(); 67 BASE_NS::weak_ptr<IBenchmarkType> weak(object); 68 for (auto _ : state) { 69 auto p = weak.lock(); 70 benchmark::DoNotOptimize(p); 71 } 72 } 73 74 BENCHMARK(SharedPtrConstruct); 75 BENCHMARK(SharedPtrCopy); 76 BENCHMARK(SharedPtrMove); 77 BENCHMARK(WeakPtrConstruct); 78 BENCHMARK(WeakPtrLock); 79 80 } // namespace benchmarks 81 META_END_NAMESPACE() 82