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

View File

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

View File

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