简体   繁体   English

C++ 数组与向量排序(在我的情况下,向量比数组慢约 2.5 倍(无优化))

[英]C++ Array vs Vector Sorting (vector ~2.5x slower than array in my case (No Optimization))

I am doing an insertion sort on of 100000 elements.我正在对100000元素进行插入排序。 I have written two functions.我写了两个函数。

1- It is in which I copy the vector given to sort into an array then I apply sort and then I copy the array back to a vector for return. 1-我将给定的向量复制到数组中,然后应用排序,然后将数组复制回向量以供返回。

2- It is in which I apply the sort on the given vector and then returns it. 2-我在给定的向量上应用排序然后返回它。

Now according to my knowledge vectors are also dynamically created an array and the difference of C++ Vector [] operator speed should be nonexistent or atleast not this much.现在根据我的知识向量也动态创建了一个数组,C++ Vector []操作符速度的差异应该不存在或至少没有这么多。 So the method 2 should be faster than method 1 .所以方法2应该比方法1快。 But to my surprise It is opposite.但令我惊讶的是,情况正好相反。 I am trying to find a concrete answer rather than just array is faster.我试图找到一个具体的答案,而不仅仅是数组更快。 :) :)

Compiler version => gcc version 6.2.0 (Rev2, Built by MSYS2 project) g++ main.cpp -o main编译器版本 => gcc 版本 6.2.0(Rev2,由 MSYS2 项目构建)g++ main.cpp -o main

#include <time.h>
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

vector<long> InsertionSort1(vector<long> nums){
    int vsize = nums.size();
    long* arr = new long[vsize];
    int swap, j;

    // Coping the vector to an array
    for(int i=0;i<vsize;i++){
        arr[i] = nums[i];
    }

    //sorting
    for(int i=1;i<vsize;i++){
        swap = arr[i];
        j = i-1;
        while(j>=0 && arr[j] > swap){
            arr[j+1] = arr[j];
            j--; 
        }

        arr[j+1] = swap;   

    }

    // Coping the array back to vector
    for(int i=0;i<vsize;i++){
        nums[i] = arr[i];
    }

    return nums;
}

vector<long> InsertionSort2(vector<long> nums){
    int vsize = nums.size();
    int swap, j;

    for(int i=1;i<vsize;i++){
        swap = nums[i];
        j = i-1;
        while(j>=0 && nums[j] > swap){
            nums[j+1] = nums[j];
            j--; 
        }

        nums[j+1] = swap;   

    }

    return nums;
}

int main(){

    vector<long> entries;
    for(int i=0;i<100000;i++){
        entries.push_back(rand()%100000);
    }

    double start = time(0);
    InsertionSort1(entries);
    double end = time(0);

    cout<<"With Array => "<<end-start<<endl;

    start = time(0);
    InsertionSort2(entries);
    end = time(0);

    cout<<"With Vector => "<<end-start<<endl;
}

The result of above Code is:上面代码的结果是:

With Array 100000 => 8
With Vector 100000 => 19

I think it is a good thing to measure performance!我认为衡量性能是一件好事! std::chrono::high_resolution_clock or google benchmark are better suited tools for the job. std::chrono::high_resolution_clockgoogle benchmark是更适合这项工作的工具。

You need to enable a proper optimisation level, say -O3 in order to have meaningful results.您需要启用适当的优化级别,例如-O3以获得有意义的结果。

Why do you like to implement a sorting algorithm.为什么喜欢实现排序算法。 I compared your approch with std::sort ?我将您的方法与std::sort For academic purposes this is fine, otherwise you need to have a good reason and a measured proof, that you did a better job.出于学术目的,这很好,否则你需要有充分的理由和经过衡量的证据,证明你做得更好。

With Array => 2.55608数组 => 2.55608

With Vector => 1.74857向量 => 1.74857

With std::sort => 0.00657156使用 std::sort => 0.00657156

#include <time.h>
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

vector<long> InsertionSort1(vector<long> nums){
    int vsize = nums.size();
    long* arr = new long[vsize];
    int swap, j;

    // Coping the vector to an array
    for(int i=0;i<vsize;i++){
        arr[i] = nums[i];
    }

    //sorting
    for(int i=1;i<vsize;i++){
        swap = arr[i];
        j = i-1;
        while(j>=0 && arr[j] > swap){
            arr[j+1] = arr[j];
            j--; 
        }

        arr[j+1] = swap;   

    }

    // Coping the array back to vector
    for(int i=0;i<vsize;i++){
        nums[i] = arr[i];
    }
    // delete of is missing arr;
    return nums;
}

vector<long> InsertionSort2(vector<long> nums){
    int vsize = nums.size();
    int swap, j;

    for(int i=1;i<vsize;i++){
        swap = nums[i];
        j = i-1;
        while(j>=0 && nums[j] > swap){
            nums[j+1] = nums[j];
            j--; 
        }

        nums[j+1] = swap;   

    }

    return nums;
}

int main(){

    vector<long> entries;
    for(int i=0;i<100000;i++){
        entries.push_back(rand()%100000);
    }

    double start = time(0);
    InsertionSort1(entries);
    double end = time(0);

    cout<<"With Array => "<<end-start<<endl;

    start = time(0);
    InsertionSort2(entries);
    end = time(0);

    cout<<"With Vector => "<<end-start<<endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM