小编Ale*_*kov的帖子

如何提高C ++中的std :: set_intersection性能?

在C ++中使用std :: set和Python中使用set()进行实验期间,我遇到了无法解释的性能问题。在C ++中设置交集至少要比Python慢​​3倍。

因此,有人能指出我可以对C ++代码进行的优化和/或解释Python如何更快地做到这一点吗?

我希望他们都可以在set有序的情况下使用O(n)复杂度的相似算法。但是Python可能会做一些优化,以使其系数变小。

set_bench.cc

#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#include <chrono>
#include <functional>
#include <thread>

void elapsed(std::function<void()> f, const std::string& s)
{
    auto start = std::chrono::steady_clock::now();
    f();
    std::chrono::duration<double> elapsed = std::chrono::steady_clock::now() - start;
    std::cout << s << " " << elapsed.count() << " seconds" << std::endl;
}

template <typename T>
void fill_set(std::set<T>& s, T start, T end, T step)
{
    for (T i = start; i < end; i += step) { …
Run Code Online (Sandbox Code Playgroud)

c++ performance hashtable set

4
推荐指数
1
解决办法
375
查看次数

标签 统计

c++ ×1

hashtable ×1

performance ×1

set ×1