chrisg
chrisg

Reputation: 200

what is faster, std::sort then set_intersection, or std::unordered_map with double for-loop find?

what is a faster?

sort two vectors and then do set_intersection... or

auto intersection = std::make_shared<MediaItems>();
std::sort(m_currentSearchResults->begin(), m_currentSearchResults->end());
std::sort(graphSearchResultMediaItems->begin(), graphSearchResultMediaItems->end());
std::set_intersection(
    m_currentSearchResults->begin(), m_currentSearchResults->end(),
    graphSearchResultMediaItems->begin(), graphSearchResultMediaItems->end(),
    std::back_inserter(*intersection));
std::swap(intersection, m_currentSearchResults);

or make them hashsets of the keys, and the do a find for-for-loop, then build the result by reverse lookup on the key?

auto projectKey = [](const auto& val) {return val.Key(); };
auto intersection = std::make_shared<MediaItems>();
std::unordered_set<mediaItemKey_t> currentRes;
std::unordered_set<mediaItemKey_t> graphRes;
std::ranges::for_each(std::views::transform(*m_currentSearchResults, projectKey), [&currentRes](const auto& val) { currentRes.emplace(val); });
std::ranges::for_each(std::views::transform(*graphSearchResultMediaItems, projectKey), [&graphRes](const auto& val) { graphRes.emplace(val); });
std::ranges::copy(
    currentRes
    | std::views::filter([graphRes](const auto& val) {return graphRes.find(val) != graphRes.end(); })
    | std::views::transform([strong_this](const auto& val) {return strong_this->m_mediaCollection->GetMediaItemByKey(val); }),

Upvotes: 0

Views: 100

Answers (0)

Related Questions