c++의 STL 성능이 얼마나 좋을까요?

gurugio의 이미지


STL의 성능이 C의 RTL과 별 차이가 없다는 소문?이 있어서
요런 소스를 한번 만들어봤습니다.

vector, list 컨테이너를 순차적으로 접근하는 시간과
그냥 int 배열을 접근하는 시간을 재보았습니다.

-O2 최적화를 해보아도 최적화 없이 해보아도 연속적인 메모리 접근에 대해서
vector 컨테이너가 int 배열보다 조금 더 빠르네요..

그냥 memcpy를 호출해서 int 배열을 복사하는 것보다 더 빠르다니
STL을 만드신 분들을 존경할 수 밖에 없는것 같습니다.

그런데 제가 제대로 비교하고 있는건지 모르겠습니다.
너무 단순한 연산을 했나요?
랜덤하게 접근하는 연산을 비교하면 어떨지 모르겠습니다.

어쨌든 저는 임베디드 환경에서도 C++ 이 사용되면 좋겠다라는 생각을 갖고
C++ 공부를 시작하게 되었습니다.
..역시 좀 어렵지만요.. ;-)

혹시 이거 낚시글이 되나요?

실행 결과입니다.
==============
gurugio@gurugio-linux:~/study/acc/6-3$ g++ main.cpp
gurugio@gurugio-linux:~/study/acc/6-3$ ./a.out
vector copy (dynamic allocated) : 0:4492
vector copy (static allocated) : 0:50
list copy (dynamic allocated) : 0:16334
list copy (static allocated) : 0:1999
integer copy (static allocated) : 0:66

gurugio@gurugio-linux:~/study/acc/6-3$ g++ -O2 main.cpp
gurugio@gurugio-linux:~/study/acc/6-3$ ./a.out
vector copy (dynamic allocated) : 0:1183
vector copy (static allocated) : 0:47
list copy (dynamic allocated) : 0:7961
list copy (static allocated) : 0:263
integer copy (static allocated) : 0:53
================

다음은 소스입니다.
===================================================

#include
#include
#include
#include

extern "C" {
#include
#include
}

using std::vector;
using std::cout;
using std::endl;
using std::list;

vector::iterator alloc_vec(vector& v)
{
v.push_back(0);
vector::iterator end = v.end();
return end-1;
}

int main(void)
{
struct timeval s, e;

/////////////////////////////
vector u(1000, 100);
vector v;

gettimeofday(&s, NULL);

for (int i = 0; i < 100; i++)
copy(u.begin(), u.end(), back_inserter(v));
gettimeofday(&e, NULL);

cout << "vector copy (dynamic allocated) : " << (int)(e.tv_sec-s.tv_sec) << ":" << (int)(e.tv_usec-s.tv_usec) << endl;

//////////////////////////////
vector u2(1000, 100);
vector v2(1000, 0);

gettimeofday(&s, NULL);
for (int i = 0; i < 100; i++)
copy(u2.begin(), u2.end(), v2.begin());
gettimeofday(&e, NULL);

cout << "vector copy (static allocated) : " << (int)(e.tv_sec-s.tv_sec) << ":" << (int)(e.tv_usec-s.tv_usec) << endl;

///////////////////////////////
list u3(1000, 100);
list v3;

gettimeofday(&s, NULL);
for (int i = 0; i < 100; i++)
copy(u3.begin(), u3.end(), back_inserter(v3));
gettimeofday(&e, NULL);

cout << "list copy (dynamic allocated) : " << (int)(e.tv_sec-s.tv_sec) << ":" << (int)(e.tv_usec-s.tv_usec) << endl;

///////////////////////////////
list u4(1000, 100);
list v4(1000, 0);

gettimeofday(&s, NULL);
for (int i = 0; i < 100; i++)
copy(u3.begin(), u3.end(), v3.begin());
gettimeofday(&e, NULL);

cout << "list copy (static allocated) : " << (int)(e.tv_sec-s.tv_sec) << ":" << (int)(e.tv_usec-s.tv_usec) << endl;

/////////////////////////////////
int u5[1000];
int v5[1000];

for (int i = 0; i < 1000; i++)
u5[i] = 100;

gettimeofday(&s, NULL);
for (int i = 0; i < 100; i++)
memcpy(v5, u5, sizeof(int)*1000);
gettimeofday(&e, NULL);

cout << "integer copy (static allocated) : " << (int)(e.tv_sec-s.tv_sec) << ":" << (int)(e.tv_usec-s.tv_usec) << endl;

return 0;
}

appler의 이미지

읽어 보셨는지 모르지만...

http://kldp.org/node/29882

이런글도 있었답니다..

C++ 열심히 했으면.. ㅠㅠ


laziness, impatience, hubris

不恥下問 - 진정으로 대화를 원하면 겸손하게 모르는 것은 모른다고 말하는 용기가 필요하다.


laziness, impatience, hubris

不恥下問 - 진정으로 대화를 원하면 겸손하게 모르는 것은 모른다고 말하는 용기가 필요하다.

klyx의 이미지

vector는 실제로 메모리상에서도 배열과 동일하게 연속으로 배치되어있는 걸로 알고있습니다.
그렇기 때문에 접근속도 자체는 별 차이가 없는게 당연할지도 모르겠네요.
그리고 이미 임베디드에서도 C++이 쓰이고 있는 걸로 알고있습니다. Qt가 임베디드용 라이브러리가 있죠.