본문 바로가기

프로그래밍

C++ 벡터와 array 속도 차이

100000개 read 기준

array : 0.0002061s
vector : 0.0240628s

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <chrono>
#include <ctime>

using namespace std;

string temp;

long fibonacci(int n) {
	if (n < 3) return 1;
	return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
	int array[100000];
	vector<int> vector2;
	for (int i = 0; i < 100000; i++) {
		array[i] = i;
		vector2.push_back(i);
	}
		chrono::time_point<chrono::high_resolution_clock> start, end;
		start = chrono::high_resolution_clock::now();
		for (int i = 0; i < 100000; i++) {
			int x = array[i];
		}
		end = chrono::high_resolution_clock::now();
		chrono::duration<double> elapsed_seconds = end - start;
		cout << "array : "<< elapsed_seconds.count() << "s" << endl;

		chrono::time_point<chrono::high_resolution_clock> start2, end2;
		start2 = chrono::high_resolution_clock::now();
		for (int i = 0; i < 100000; i++) {
			int x = vector2[i];
		}
		end2 = chrono::high_resolution_clock::now();
		chrono::duration<double> elapsed_seconds2 = end2 - start2;
		cout << "vector : " << elapsed_seconds2.count() << "s" << endl;

	while (1) {}
	return 0;
}

'프로그래밍' 카테고리의 다른 글

날짜체크 알고리즘  (0) 2019.08.27
C++ 벡터와 array 속도 차이  (0) 2019.08.27