Skip to content

Commit

Permalink
Merge pull request #6432 from nikwit/speed-up-list-obs-ids
Browse files Browse the repository at this point in the history
Speed up `list_observation_ids()`
  • Loading branch information
knelli2 authored Jan 9, 2025
2 parents 49286a3 + 11c595f commit 424f543
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/IO/H5/VolumeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <optional>
#include <ostream>
#include <string>
#include <unordered_map>
#include <vector>

#include "DataStructures/DataVector.hpp"
Expand Down Expand Up @@ -417,8 +418,13 @@ std::vector<size_t> VolumeData::list_observation_ids() const {
std::vector<size_t> obs_ids{
boost::make_transform_iterator(names.begin(), helper),
boost::make_transform_iterator(names.end(), helper)};
alg::sort(obs_ids, [this](const size_t lhs, const size_t rhs) {
return this->get_observation_value(lhs) < this->get_observation_value(rhs);
// pre-compute the observation values as they are expensive to evaluate
std::unordered_map<size_t, double> obs_values{obs_ids.size()};
for (const auto& id : obs_ids) {
obs_values[id] = this->get_observation_value(id);
}
alg::sort(obs_ids, [&obs_values](const size_t lhs, const size_t rhs) {
return obs_values[lhs] < obs_values[rhs];
});
return obs_ids;
}
Expand Down

0 comments on commit 424f543

Please sign in to comment.