본문으로 바로가기

이미 요소의 갯수를 알고 있을때의 속도 테스트를 해봄..


테스트 비교는..

 

1. Raw Array..

2. C로 구현된 Vector (https://github.com/goldsborough/vector), 

3. C++로 구현된 FBVctor (https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md), 

4. C++의 Standard Vector

#include <iostream>
#include <chrono>
 
#include <folly/memory/Malloc.h>
#include <folly/FBVector.h>
 
#include <vector>
 
#include <vector/vector.h>
 
int main(void)
{
    {
        std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
 
        unsigned int *vec = (unsigned int *)malloc(1000000000 * sizeof(unsigned int));
 
        for(unsigned int i=0; i<1000000000; i++) {
            vec[i] = i;
        }
 
        std::chrono::duration<double> sec = std::chrono::system_clock::now() - start;
        std::cout << "array: Time passed (sec) : " << sec.count() << " seconds" << std::endl;
 
        std::cout << "Press any key to next test." << std::endl;
        getchar();
         
        free(vec);
    }
 
    {
        std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
 
        Vector vec;
        vector_setup(&vec, 1000000000, sizeof(unsigned int));
 
        for(unsigned int i=0; i<1000000000; i++) {
            vector_push_back(&vec, &i);
        }
 
        std::chrono::duration<double> sec = std::chrono::system_clock::now() - start;
        std::cout << "C Vector: Time passed (sec) : " << sec.count() << " seconds" << std::endl;
 
        std::cout << "Press any key to next test." << std::endl;
        getchar();
         
        vector_clear(&vec);
        vector_destroy(&vec);
    }
 
    {
        std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
 
        folly::fbvector<unsigned int> vec;
        vec.reserve(1000000000);
        for(int i=0; i<1000000000; i++) {
            vec.push_back(i);
        }
 
        std::chrono::duration<double> sec = std::chrono::system_clock::now() - start;
        std::cout << "C++ FBVector: Time passed (sec) : " << sec.count() << " seconds" << std::endl;
 
        std::cout << "Press any key to next test." << std::endl;
        getchar();
         
        vec.clear();
        vec.shrink_to_fit();
    }
 
    {
        std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
 
        std::vector<unsigned int> vec;
        vec.reserve(1000000000);
        for(int i=0; i<1000000000; i++) {
            vec.push_back(i);
        }
 
        std::chrono::duration<double> sec = std::chrono::system_clock::now() - start;
        std::cout << "C++ Vector: Time passed (sec) : " << sec.count() << " seconds" << std::endl;
 
        std::cout << "Press any key to exit." << std::endl;
        getchar();
 
        vec.clear();
        vec.shrink_to_fit();
    }
 
    return 0;
}

 

array: Time passed (sec) : 2.27096 seconds
Press any key to next test.
 
C Vector: Time passed (sec) : 11.588 seconds
Press any key to next test.
 
C++ FBVector: Time passed (sec) : 6.22477 seconds
Press any key to next test.
 
C++ Vector: Time passed (sec) : 14.0583 seconds
Press any key to exit.


결과는 Raw Array 가 가장 빠름... 하지만 Vector만 놓고 봤을때는 FBVector가 승리..