C ++数组与std :: vector和std :: array的效率 [英] efficiency of c++ arrays vs std::vector and std::array

查看:56
本文介绍了C ++数组与std :: vector和std :: array的效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我比较了分配1d数组或2d数组的不同类型,如下所示.我发现使用new运算符效率更高,也许std :: arrary和std :: vector是一个对象,它们是通用且安全的,但是会花费更多时间吗?而且,我不知道为什么调用新的外部函数比内部函数更有效?

  #include< iostream>#include< vector>#include< array>#include< ctime>无效test1(){int * arr = new int [10000];对于(int i = 0; i< 10000; ++ i){arr [i] = 3;}对于(int i = 0; i< 10000; ++ i){int a = arr [i];}删除arr;}无效test11(){int ** arr = new int * [100];对于(int i = 0; i< 100; ++ i){arr [i] = new int [100];}对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j){arr [i] [j] = 3;}}对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j){int a = arr [i] [j];}}删除[] arr;}无效test2(){std :: vector< int>arr(10000);对于(int i = 0; i< 10000; ++ i){arr [i] = 3;}对于(int i = 0; i< 10000; ++ i){int a = arr [i];}}无效test22(){std :: vector< std :: vector< int>>arr(100,std :: vector< int>(100));对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j){arr [i] [j] = 3;}}对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j){int a = arr [i] [j];}}}无效的test3(int * arr,int n){for(int i = 0; i< n; ++ i){arr [i] = 3;}for(int i = 0; i< n; ++ i){int a = arr [i];}}无效test33(int ** arr,int m,int n){for(int i = 0; i< m; ++ i){对于(int j = 0; j< n; ++ j){arr [i] [j] = 3;}}for(int i = 0; i< m; ++ i){对于(int j = 0; j< n; ++ j){int a = arr [i] [j];}}}无效test4(){std :: array< int,10000>arr;对于(int i = 0; i< 10000; ++ i){arr [i] = 3;}对于(int i = 0; i< 10000; ++ i){int a = arr [i];}}无效test44(){std :: array< std :: array< int,100> ;, 100>arr;对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j){arr [i] [j] = 3;}}对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j)int a = arr [i] [j];}}int main(){clock_t开始,结束;开始= clock();对于(int i = 0; i< 1000; ++ i){test1();}结束=时钟();std :: cout<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test11();}结束=时钟();std :: cout<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test2();}结束=时钟();std :: cout<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test22();}结束=时钟();std :: cout<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){int * arr = new int [10000];test3(arr,10000);删除arr;}结束=时钟();std :: cout<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();int ** arr = new int * [100];对于(int i = 0; i< 100; ++ i){arr [i] = new int [100];}对于(int i = 0; i< 1000; ++ i){test33(arr,100,100);}删除[] arr;结束=时钟();std :: cout<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test4();}结束=时钟();std :: cout<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test44();}结束=时钟();std :: cout<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;} 

输出为:

  90毫秒80毫秒70毫秒120毫秒50毫秒40毫秒100毫秒190毫秒 

感谢您的帮助,也许我没有正确描述我的问题,我编写了一个将多次调用的函数,此函数创建了一个数组,然后将其删除:

  void fun(){int * arr = new int [10000];//可能很大//做其他事情删除arr;} 

有人告诉我这不是很有效,因为它每次都会更新并删除,现在我有两个问题:

1.正确的内存管理方法是什么?

  int * arr =新的int [];删除arr;int ** arr = new int * [];删除[] arr; 

错了吗?也许像这样:

(int i = 0; i< n; ++ i)的

  {删除[] arr;}删除arr; 

2.对我来说,编写此功能的最佳方式是什么?

解决方案

让我们做一些工作来改进测试,并通过正确使用标准库来给它一个机会...

  #include< iostream>#include< vector>#include< array>#include< ctime>#include< memory>#include< algorithm>#include< iterator>无效test1(){自动arr = std :: make_unique< int []>(10000);std :: fill(arr.get(),arr.get()+ 10000,3);对于(int i = 0; i< 10000; ++ i){int a = arr [i];}}无效test11(){auto arr = std :: make_unique< std :: unique_ptr< int []> []>(100);对于(auto i = 0; i <100; ++ i){arr [i] = std :: make_unique< int []>(100);}对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j){arr [i] [j] = 3;}}对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j){int a = arr [i] [j];}}}无效test2(){std :: vector< int>arr(10000);std :: fill(std :: begin(arr),std :: end(arr),3);对于(int i = 0; i< 10000; ++ i){int a = arr [i];}}无效test22(){std :: vector< std :: vector< int>>arr(100,std :: vector< int>(100));std :: for_each(begin(arr),结束(arr),[](自动和内部){std :: fill(std :: begin(inner),std :: end(inner),3);});对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j){int a = arr [i] [j];}}}无效的test3(int * arr,int n){std :: fill(arr,arr + n,3);for(int i = 0; i< n; ++ i){int a = arr [i];}}无效test33(const std :: unique_ptr< std :: unique_ptr< int []> []& arr,int m,int n){for(int i = 0; i< m; ++ i){对于(int j = 0; j< n; ++ j){arr [i] [j] = 3;}}for(int i = 0; i< m; ++ i){对于(int j = 0; j< n; ++ j){int a = arr [i] [j];}}}无效test4(){std :: array< int,10000>arr;std :: fill(std :: begin(arr),std :: end(arr),3);对于(int i = 0; i< 10000; ++ i){int a = arr [i];}}无效test44(){std :: array< std :: array< int,100> ;, 100>arr;std :: for_each(begin(arr),结束(arr),[](自动和内部){std :: fill(std :: begin(inner),std :: end(inner),3);});对于(int i = 0; i< 100; ++ i){对于(int j = 0; j< 100; ++ j)int a = arr [i] [j];}}int main(){clock_t开始,结束;开始= clock();对于(int i = 0; i< 1000; ++ i){test1();}结束=时钟();std :: cout<<测试1"<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test11();}结束=时钟();std :: cout<<测试11"<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test2();}结束=时钟();std :: cout<<测试2"<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test22();}结束=时钟();std :: cout<<测试22"<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){int * arr = new int [10000];test3(arr,10000);删除[] arr;}结束=时钟();std :: cout<<测试3"<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();auto arr = std :: make_unique< std :: unique_ptr< int []> []>(100);对于(auto i = 0; i< 100; ++ i){arr [i] = std :: make_unique< int []>(100);}对于(int i = 0; i< 1000; ++ i){test33(arr,100,100);}arr.reset();结束=时钟();std :: cout<<测试33"<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test4();}结束=时钟();std :: cout<<测试4"<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;开始= clock();对于(int i = 0; i< 1000; ++ i){test44();}结束=时钟();std :: cout<<测试44"<<(双)(结束-开始)* 1000.0/CLOCKS_PER_SEC<<"ms"<<std :: endl;} 

使用-O2编译时在我的计算机上显示结果:

  test 1 0.002毫秒测试11 13.506毫秒测试2 2.753毫秒测试22 13.738毫秒测试3 1.42毫秒测试33 1.552毫秒测试4 0毫秒测试44 0毫秒 

我们还要注意,数组是小"的,并且被重复分配和释放.如果您可以重新使用缓冲区,那么时间上的差异将完全消失.

还请注意:test33速度很快,因为它永远不会重新分配内存-您正在重新使用缓冲区.

I compared different type of allocating a 1d-array or 2d-array as follows. I found that using new operator is more efficient, maybe std::arrary and std::vector is a object, they are generic and safe but more time? Morever, I do not know why call new outside function is more efficent than that inside function?

#include <iostream>
#include <vector>
#include <array>
#include <ctime>


void test1 () {
int *arr = new int[10000];

for (int i=0; i<10000; ++i) {
    arr[i] = 3;
}

for (int i=0; i<10000; ++i) {
    int a = arr[i];
}
delete arr;
}

void test11 () {
int **arr = new int*[100];
for (int i=0; i<100; ++i) {
    arr[i] = new int[100];
}

for (int i=0; i<100; ++i) {
    for (int j=0; j<100; ++j) {
        arr[i][j] = 3;
    }
}

for (int i=0; i<100; ++i) {
    for (int j=0; j<100; ++j) {
        int a = arr[i][j];
    }
}

delete [] arr;
}


void test2() {
std::vector<int> arr(10000);

for (int i=0; i<10000; ++i) {
    arr[i] = 3;
}

for (int i=0; i<10000; ++i) {
     int a = arr[i];
}
}

void test22() {
std::vector<std::vector<int> > arr(100, std::vector<int>(100));

for (int i=0; i<100; ++i) {
    for (int j=0; j<100; ++j) {
        arr[i][j] = 3;
    }
}

for (int i=0; i<100; ++i) {
    for (int j=0; j<100; ++j) {
        int a = arr[i][j];
    }
}
}



void test3(int *arr, int n) {
for (int i=0; i<n; ++i) {
    arr[i] = 3;
}
for (int i=0; i<n; ++i) {
     int a = arr[i];
}
}

void test33(int **arr, int m, int n) {
for (int i=0; i<m; ++i) {
    for (int j=0; j<n; ++j) {
        arr[i][j] = 3;
    }
}

for (int i=0; i<m; ++i) {
    for (int j=0; j<n; ++j) {
        int a = arr[i][j];
    }
}

}

void test4() {
std::array<int, 10000> arr;
for (int i=0; i<10000; ++i) {
    arr[i] = 3;
}
for (int i=0; i<10000; ++i) {
     int a = arr[i];
}
}

void test44() {
std::array<std::array<int, 100>, 100> arr;
for (int i=0; i<100; ++i) {
    for (int j=0; j<100; ++j) {
         arr[i][j] = 3;
    }
}
for (int i=0; i<100; ++i) {
    for (int j=0; j<100; ++j)
     int a = arr[i][j];
}
}


int main() {
clock_t start, end;
start = clock();
for (int i=0; i<1000; ++i) {
    test1();
}
end = clock();
std::cout << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

start = clock();
for (int i=0; i<1000; ++i) {
    test11();
}
end = clock();
std::cout << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

start = clock();
for (int i=0; i<1000; ++i) {
    test2();
}
end = clock();
std::cout << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

start = clock();
for (int i=0; i<1000; ++i) {
    test22();
}
end = clock();
std::cout << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

start = clock();
for (int i=0; i<1000; ++i) {
    int *arr = new int[10000];
    test3(arr, 10000);
    delete arr;
}
end = clock();
std::cout << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

start = clock();
int **arr = new int*[100];
for (int i=0; i<100; ++i) {
    arr[i] = new int[100];
}

for (int i=0; i<1000; ++i) {
    test33(arr, 100, 100);
}
delete [] arr;
end = clock();
std::cout << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

start = clock();
for (int i=0; i<1000; ++i) {
    test4();
}
end = clock();
std::cout << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

start = clock();
for (int i=0; i<1000; ++i) {
    test44();
}
end = clock();
std::cout << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

}

the output is:

90 ms
80 ms
70 ms
120 ms
50 ms
40 ms
100 ms
190 ms

Thanks for your help, maybe I did not describe my question correctly, I write a function that will be calling many times, this function new a array then delete it:

void fun() {
   int *arr = new int[10000]; //maybe very big
   //todo something else
   delete arr; 
}

someone tell me it's not efficient, because it new and delete every time, now I have two question:

1. what is the correct way to memory management?

int *arr = new int[]; delete arr;
int **arr = new int*[]; delete [] arr;

wrong? maybe like this:

for (int i=0; i<n; ++i){
  delete [] arr;
}
delete arr;

2. what is the best way for me to write this function

解决方案

Let's do some work to improve the test, and give the standard library a chance by using it properly...

#include <iostream>
#include <vector>
#include <array>
#include <ctime>
#include <memory>
#include <algorithm>
#include <iterator>

void test1 () {
    auto arr = std::make_unique<int[]>(10000);
    std::fill(arr.get(), arr.get() + 10000, 3);

    for (int i=0; i<10000; ++i) {
        int a = arr[i];
    }
}

void test11 () {
    auto arr = std::make_unique<std::unique_ptr<int[]>[]>(100);
    for (auto i = 0 ; i < 100 ; ++i) {
        arr[i] = std::make_unique<int[]>(100);
    }

    for (int i=0; i<100; ++i) {
        for (int j=0; j<100; ++j) {
            arr[i][j] = 3;
        }
    }

    for (int i=0; i<100; ++i) {
        for (int j=0; j<100; ++j) {
            int a = arr[i][j];
        }
    }

}


void test2() {
    std::vector<int> arr(10000);
    std::fill(std::begin(arr), std::end(arr), 3);

    for (int i=0; i<10000; ++i) {
        int a = arr[i];
    }
}

void test22() {
    std::vector<std::vector<int> > arr(100, std::vector<int>(100));
    std::for_each(begin(arr),
                  end(arr),
                  [](auto& inner) {
                      std::fill(std::begin(inner), std::end(inner), 3);
                  });

    for (int i=0; i<100; ++i) {
        for (int j=0; j<100; ++j) {
            int a = arr[i][j];
        }
    }
}



void test3(int *arr, int n) {
    std::fill(arr, arr + n, 3);

    for (int i=0; i<n; ++i) {
        int a = arr[i];
    }
}

void test33(const std::unique_ptr<std::unique_ptr<int[]>[]>& arr, int m, int n) {
    for (int i=0; i<m; ++i) {
        for (int j=0; j<n; ++j) {
            arr[i][j] = 3;
        }
    }

    for (int i=0; i<m; ++i) {
        for (int j=0; j<n; ++j) {
            int a = arr[i][j];
        }
    }

}

void test4() {
    std::array<int, 10000> arr;
    std::fill(std::begin(arr), std::end(arr), 3);

    for (int i=0; i<10000; ++i) {
        int a = arr[i];
    }
}

void test44() {
    std::array<std::array<int, 100>, 100> arr;
    std::for_each(begin(arr),
                  end(arr),
                  [](auto& inner) {
                      std::fill(std::begin(inner), std::end(inner), 3);
                  });

    for (int i=0; i<100; ++i) {
        for (int j=0; j<100; ++j)
            int a = arr[i][j];
    }
}


int main() {
    clock_t start, end;
    start = clock();
    for (int i=0; i<1000; ++i) {
        test1();
    }
    end = clock();
    std::cout << "test 1 " << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

    start = clock();
    for (int i=0; i<1000; ++i) {
        test11();
    }
    end = clock();
    std::cout << "test 11 " << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

    start = clock();
    for (int i=0; i<1000; ++i) {
        test2();
    }
    end = clock();
    std::cout << "test 2 " << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

    start = clock();
    for (int i=0; i<1000; ++i) {
        test22();
    }
    end = clock();
    std::cout << "test 22 " << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

    start = clock();
    for (int i=0; i<1000; ++i) {
        int *arr = new int[10000];
        test3(arr, 10000);
        delete [] arr;
    }
    end = clock();
    std::cout << "test 3 " << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

    start = clock();
    auto arr = std::make_unique<std::unique_ptr<int[]>[]>(100);
    for (auto i = 0 ; i < 100 ; ++i) {
        arr[i] = std::make_unique<int[]>(100);
    }

    for (int i=0; i<1000; ++i) {
        test33(arr, 100, 100);
    }
    arr.reset();

    end = clock();
    std::cout << "test 33 " << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

    start = clock();
    for (int i=0; i<1000; ++i) {
        test4();
    }
    end = clock();
    std::cout << "test 4 " << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

    start = clock();
    for (int i=0; i<1000; ++i) {
        test44();
    }
    end = clock();
    std::cout << "test 44 " << (double)(end - start) * 1000.0 / CLOCKS_PER_SEC << " ms" << std::endl;

}

results on my computer when compiled with -O2:

test 1 0.002 ms
test 11 13.506 ms
test 2 2.753 ms
test 22 13.738 ms
test 3 1.42 ms
test 33 1.552 ms
test 4 0 ms
test 44 0 ms

Let's also note that the arrays are "small" and are being allocated and deallocated repeatedly. If you can re-use the buffers, then the difference in timing will vanish completely.

Also note: test33 is fast because it never reallocates memory - you're re-using the buffer.

这篇关于C ++数组与std :: vector和std :: array的效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆