Use efficient string concatenation (#1488)

std::string::operator+ is very inefficient
master
Andre Gaschler 2019-01-02 13:57:02 +01:00 committed by Wally B. Feed
parent 5182dd1bf9
commit 752fb9507f
3 changed files with 16 additions and 14 deletions

View File

@ -20,6 +20,7 @@
#include <numeric> #include <numeric>
#include <string> #include <string>
#include "absl/strings/str_cat.h"
#include "cartographer/common/port.h" #include "cartographer/common/port.h"
#include "glog/logging.h" #include "glog/logging.h"
@ -37,10 +38,8 @@ std::string Histogram::ToString(const int buckets) const {
const float max = *std::max_element(values_.begin(), values_.end()); const float max = *std::max_element(values_.begin(), values_.end());
const float mean = const float mean =
std::accumulate(values_.begin(), values_.end(), 0.f) / values_.size(); std::accumulate(values_.begin(), values_.end(), 0.f) / values_.size();
std::string result = "Count: " + std::to_string(values_.size()) + std::string result = absl::StrCat("Count: ", values_.size(), " Min: ", min,
" Min: " + std::to_string(min) + " Max: ", max, " Mean: ", mean);
" Max: " + std::to_string(max) +
" Mean: " + std::to_string(mean);
if (min == max) { if (min == max) {
return result; return result;
} }
@ -60,8 +59,8 @@ std::string Histogram::ToString(const int buckets) const {
} }
} }
total_count += count; total_count += count;
result += "\n[" + std::to_string(lower_bound) + ", " + absl::StrAppend(&result, "\n[", lower_bound, ", ", upper_bound,
std::to_string(upper_bound) + ((i + 1 == buckets) ? "]" : ")"); i + 1 == buckets ? "]" : ")");
constexpr int kMaxBarChars = 20; constexpr int kMaxBarChars = 20;
const int bar = const int bar =
(count * kMaxBarChars + values_.size() / 2) / values_.size(); (count * kMaxBarChars + values_.size() / 2) / values_.size();
@ -69,10 +68,10 @@ std::string Histogram::ToString(const int buckets) const {
for (int i = 0; i != kMaxBarChars; ++i) { for (int i = 0; i != kMaxBarChars; ++i) {
result += (i < (kMaxBarChars - bar)) ? " " : "#"; result += (i < (kMaxBarChars - bar)) ? " " : "#";
} }
result += "\tCount: " + std::to_string(count) + " (" + absl::StrAppend(&result, "\tCount: ", count, " (",
std::to_string(count * 1e2f / values_.size()) + "%)"; count * 1e2f / values_.size(), "%)",
result += "\tTotal: " + std::to_string(total_count) + " (" + "\tTotal: ", total_count, " (",
std::to_string(total_count * 1e2f / values_.size()) + "%)"; total_count * 1e2f / values_.size(), "%)");
lower_bound = upper_bound; lower_bound = upper_bound;
} }
return result; return result;

View File

@ -21,6 +21,7 @@
#include "Eigen/Core" #include "Eigen/Core"
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "cartographer/common/lua_parameter_dictionary.h" #include "cartographer/common/lua_parameter_dictionary.h"
#include "cartographer/common/math.h" #include "cartographer/common/math.h"
#include "cartographer/io/draw_trajectories.h" #include "cartographer/io/draw_trajectories.h"
@ -236,7 +237,7 @@ PointsProcessor::FlushResult XRayPointsProcessor::Flush() {
for (size_t i = 0; i < floors_.size(); ++i) { for (size_t i = 0; i < floors_.size(); ++i) {
WriteVoxels( WriteVoxels(
aggregations_[i], aggregations_[i],
file_writer_factory_(output_filename_ + std::to_string(i) + ".png") file_writer_factory_(absl::StrCat(output_filename_, i, ".png"))
.get()); .get());
} }
} }

View File

@ -16,6 +16,8 @@
#include "cartographer/sensor/internal/trajectory_collator.h" #include "cartographer/sensor/internal/trajectory_collator.h"
#include "absl/strings/str_cat.h"
namespace cartographer { namespace cartographer {
namespace sensor { namespace sensor {
@ -71,7 +73,8 @@ void TrajectoryCollator::RegisterMetrics(
metrics::Counter* TrajectoryCollator::GetOrCreateSensorMetric( metrics::Counter* TrajectoryCollator::GetOrCreateSensorMetric(
const std::string& sensor_id, int trajectory_id) { const std::string& sensor_id, int trajectory_id) {
const std::string map_key = sensor_id + "/" + std::to_string(trajectory_id); const std::string trajectory_id_str = absl::StrCat(trajectory_id);
const std::string map_key = absl::StrCat(sensor_id, "/", trajectory_id_str);
auto metrics_map_itr = metrics_map_.find(map_key); auto metrics_map_itr = metrics_map_.find(map_key);
if (metrics_map_itr != metrics_map_.end()) { if (metrics_map_itr != metrics_map_.end()) {
@ -80,8 +83,7 @@ metrics::Counter* TrajectoryCollator::GetOrCreateSensorMetric(
LOG(INFO) << "Create metrics handler for key: " << map_key; LOG(INFO) << "Create metrics handler for key: " << map_key;
auto new_counter = collator_metrics_family_->Add( auto new_counter = collator_metrics_family_->Add(
{{"sensor_id", sensor_id}, {{"sensor_id", sensor_id}, {"trajectory_id", trajectory_id_str}});
{"trajectory_id", std::to_string(trajectory_id)}});
metrics_map_[map_key] = new_counter; metrics_map_[map_key] = new_counter;
return new_counter; return new_counter;