From 2f1125bc598f415a72128c048c4eef2331a83341 Mon Sep 17 00:00:00 2001 From: Prateek Machiraju Date: Fri, 28 Jun 2019 14:16:35 -0400 Subject: [PATCH] add emplace_back test --- src/bench/bench_vector_creation.cpp | 45 ++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/bench/bench_vector_creation.cpp b/src/bench/bench_vector_creation.cpp index cc9a42d..7f24668 100644 --- a/src/bench/bench_vector_creation.cpp +++ b/src/bench/bench_vector_creation.cpp @@ -11,6 +11,30 @@ struct ConstrainedPose { double max_velocity = 0.0; double min_acceleration = 0.0; double max_acceleration = 0.0; + + ConstrainedPose() {} + + ConstrainedPose(const S& state, double distance, double max_velocity, double min_acceleration, + double max_acceleration) { + this->state = state; + this->distance = distance; + this->max_velocity = max_velocity; + this->min_acceleration = min_acceleration; + this->max_acceleration = max_acceleration; + } + + ConstrainedPose(ConstrainedPose& other) = default; + ConstrainedPose& operator=(ConstrainedPose& other) { + //std::cout << "Copy operator called"; + this->state = other.state; + this->distance = other.distance; + this->max_velocity = other.max_velocity; + this->min_acceleration = other.min_acceleration; + this->max_acceleration = other.max_acceleration; + return *this; + } + + ConstrainedPose& operator=(ConstrainedPose&& other) = default; }; static void BM_VectorReserveAndPushBack(benchmark::State& state) { @@ -19,19 +43,32 @@ static void BM_VectorReserveAndPushBack(benchmark::State& state) { vector.reserve(kVectorSize); for (int i = 0; i < kVectorSize; i++) { - vector.push_back({fl::Pose2dWithCurvature{}, 1.0 * i, 1.0 * i, 1.0 * i}); + vector.push_back({fl::Pose2dWithCurvature{}, 1.0 * i, 1.0 * i, 1.0 * i, 1.0 * i}); } } } -static void BM_VectorConstructor(benchmark::State& state) { +static void BM_VectorReserveAndEmplaceBack(benchmark::State& state) { + for (auto _ : state) { + std::vector> vector; + vector.reserve(kVectorSize); + + for (int i = 0; i < kVectorSize; i++) { + vector.emplace_back(fl::Pose2dWithCurvature{}, 1.0 * i, 1.0 * i, 1.0 * i, 1.0 * i); + } + } +} + +static void BM_VectorConstructorAndAssignIndices(benchmark::State& state) { for (auto _ : state) { std::vector> vector{kVectorSize}; for (int i = 0; i < kVectorSize; i++) { - vector[i] = {fl::Pose2dWithCurvature{}, 1.0 * i, 1.0 * i, 1.0 * i}; + vector[i] = ConstrainedPose(fl::Pose2dWithCurvature{}, 1.0 * i, 1.0 * i, + 1.0 * i, 1.0 * i); } } } +BENCHMARK(BM_VectorReserveAndEmplaceBack)->Arg(100)->Complexity()->Unit(benchmark::kMillisecond); BENCHMARK(BM_VectorReserveAndPushBack)->Arg(100)->Complexity()->Unit(benchmark::kMillisecond); -BENCHMARK(BM_VectorConstructor)->Arg(100)->Complexity()->Unit(benchmark::kMillisecond); \ No newline at end of file +BENCHMARK(BM_VectorConstructorAndAssignIndices)->Arg(100)->Complexity()->Unit(benchmark::kMillisecond); \ No newline at end of file