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
new[]has to be released withdelete[].std::vector.