C++ array and vector access execution performance comparison

When I was brushing LeetCode recently, I found that for frequent operations on a set of data, the efficiency of the array is much faster than that of the vector. I was really confused by the vector, so I did an experiment.

Execute on windows

First , I used vs2015 on win10 to perform 100,000 operations on the array and vector respectively , and the results are obvious.
Array and vector efficiency comparison
Here is the running code:


#include<iostream>
#include<windows.h>
#include< vector >
#include< algorithm >
#include< string >
using namespace std;
int main()
{
    
    
	DWORD start_time2 = GetTickCount();
	int arr2[10000];
	for (int i = 0; i < 100000; i++) {
    
    
		//if(arr2[i% 10]);
		arr2[i % 10000]++;
	}
	DWORD end_time2 = GetTickCount();
	cout << "array run time is " << (end_time2 - start_time2) << "ms." << endl;

	DWORD start_time1 = GetTickCount(); 
	vector<int> vect(10000);
	for (int i = 0; i < 100000; i++) {
    
    
		vect[i % 10000]++;
		//if(vect[i % 10000]);
	}
	DWORD end_time1 = GetTickCount(); 
	cout << "vector run time is " << (end_time1 - start_time1) << "ms." << endl;
	system("pause");
	return 0;
}

The main time-consuming aspect is the speed of access . You can change the calculation in the loop to access (notes), and the result is similar.

We adjusted the data to 1 million , and the result is:
Array and vector efficiency comparison

Transfer to 10 million :
Array and vector efficiency comparison

Run on windows, the greater the number, the greater the gap, which may be related to the compilation environment and computer configuration.

Execute on linux

Next, let's operate on Linux, and the results are as follows:
execute 10 million for array and vector respectively :
Array and vector efficiency comparison
adjust to 100 million times: It
Array and vector efficiency comparison
may be the cloud server, the reason is relatively powerful, the gap is not very obvious. The
running code is as follows:

#include<iostream>
#include<windows.h>
//#include< algorithm >
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
using namespace std;

int getTime(){
    
    
    struct timeval tv;
    gettimeofday(&tv,NULL);
	return tv.tv_sec*1000 + tv.tv_usec/1000;
}
int main()
{
    
    
	int temp;
	int arr2[10000];
	int start_time2 = getTime(); 
	for (int i = 0; i < 100000000; i++) {
    
    
		arr2[i % 10000]++;
		// if (arr2[i % 9]) temp++;

	}
	int end_time2 = getTime(); 
	std::cout << "array run time is " << (end_time2 - start_time2) << "ms." << endl;

    vector<int> vect(10000);
	int start_time1 = getTime(); 
	for (int i = 0; i < 100000000; i++) {
    
    
		vect[i % 10000]++;
		// if(vect[i%9]) temp++;

	}
	int end_time1 = getTime(); 
	std::cout << "vector run time is " << (end_time1 - start_time1) << "ms." << endl;


   return 0;
}

to sum up

As seen above , the data frequently accessed to perform an operation, the array than vector outstanding lot.
However , this does not mean that arrays are better than vectors. Vectors have many powerful functions and can store many types. In contrast, arrays are still too single. If you only perform simple and frequent access to a group of data, you can C++ arrays, otherwise C++ vectors are still used in most cases.

Guess you like

Origin blog.csdn.net/h799710/article/details/107544792