Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting C++;11 Lambda自定义比较器降低排序速度_Sorting_C++11_Lambda - Fatal编程技术网

Sorting C++;11 Lambda自定义比较器降低排序速度

Sorting C++;11 Lambda自定义比较器降低排序速度,sorting,c++11,lambda,Sorting,C++11,Lambda,自定义lambda比较器比普通函数c++11慢。我经历过几次。但是,我还是不明白为什么会这样。有人经历过这种情况并知道其背后的原因吗 #include <bits/stdc++.h> using namespace std; const int N = 1e4 + 1; vector<int> v(N); vector<int> sorted(N); map<int, int> counts;

自定义lambda比较器比普通函数c++11慢。我经历过几次。但是,我还是不明白为什么会这样。有人经历过这种情况并知道其背后的原因吗

    #include <bits/stdc++.h>
    using namespace std;

    const int N = 1e4 + 1;
    vector<int> v(N);
    vector<int> sorted(N);
    map<int, int> counts;
    long long start;

    void startClock() {
        start = clock();
    }

    void stopClock() {
        cout << float( clock () - start ) /  CLOCKS_PER_SEC << endl;
    }

    void copyOriginal() {
        for (int i = 0; i < N; ++i)
            sorted[i] = v[i];
    }

    void sortWLambda(map<int, int>& counts) {
        cout << "sorting with lambda" << endl;
        sort(sorted.begin(), sorted.end(), [counts](const int& a, const int& b) {
            if (*counts.find(a) != *counts.find(b)) return *counts.find(a) < *counts.find(b);
            return a < b;
        });
    }

    bool comparator(const int& a, const int& b) {
        if (*counts.find(a) != *counts.find(b)) return *counts.find(a) < *counts.find(b);
        return a < b;
    }

    void sortWoLambda() {
        cout << "sorting w/o lambda" << endl;
        sort(sorted.begin(), sorted.end(), comparator);
    }

    int main() {
        for (int i = 0; i < N; ++i) {
            int num = rand() % 1234;
            counts[num]++;
            v[i] = num;
        }

        copyOriginal();
        startClock();
        sortWLambda(counts);
        stopClock();

        copyOriginal();
        startClock();
        sortWoLambda();
        stopClock();

        return 0;
    }
#包括
使用名称空间std;
常数int N=1e4+1;
向量v(N);
向量排序(N);
地图计数;
长远的开始;
void startClock(){
开始=时钟();
}
无效秒钟(){

coutpassbyreference对lambda有影响

我试过这个

        sort(sorted.begin(), sorted.end(), [&counts](const int& a, const int& b) {
            if (*counts.find(a) != *counts.find(b)) return *counts.find(a) < *counts.find(b);
            return a < b;
        });
sort(sorted.begin()、sorted.end()、[&counts](常量int&a、常量int&b){
如果(*counts.find(a)!=*counts.find(b))返回*counts.find(a)<*counts.find(b);
返回a
现在这需要和正常功能相同的时间


这对我也有帮助!

通过引用传递对lambda产生了影响

我试过这个

        sort(sorted.begin(), sorted.end(), [&counts](const int& a, const int& b) {
            if (*counts.find(a) != *counts.find(b)) return *counts.find(a) < *counts.find(b);
            return a < b;
        });
sort(sorted.begin()、sorted.end()、[&counts](常量int&a、常量int&b){
如果(*counts.find(a)!=*counts.find(b))返回*counts.find(a)<*counts.find(b);
返回a
现在这需要和正常功能相同的时间


这对我也有帮助!

你在测试优化的构建吗?我两个都得到了0.017。
计数
正在被复制到lambda中,我不认为这会对它有多大影响,但它似乎是:是的,我发现了相同的。Visual Studio是同一时间复制还是不复制。切换到无序地图更好。谢谢!看起来像
[counts](){}
正在复制映射。通过引用传递,如
[&counts](){}
效果很好你在测试一个优化的构建吗?两者我都得到0.017。
计数
被复制到lambda中,我不认为这会对它有多大影响,但它似乎是:是的,我发现了相同的。Visual Studio是同一时间的复制还是不复制。切换到无序的地图更好。谢谢!似乎是
[counts]({}
正在复制地图。通过引用传递,如
[&counts](){}
效果良好