Skip to content

Instantly share code, notes, and snippets.

@nlguillemot
Created July 1, 2012 04:06
Show Gist options
  • Save nlguillemot/3026781 to your computer and use it in GitHub Desktop.
Save nlguillemot/3026781 to your computer and use it in GitHub Desktop.
Comparison of performance of adding elements to containers in C++
Vector took 0.18 seconds.
Preallocated Vector took 0.1 seconds.
Back pushed List took 1.38 seconds.
Front pushed List took 1 seconds.
Front pushed Pool allocated list took 0.67 seconds.
In other words,
Preallocating a vector makes it ~200% faster than not preallocating
Pushing to the front makes it ~150% faster than pushing to the back
Pushing to the front and using a pool allocator makes it ~200% faster than pushing to the back
#include <vector>
#include <list>
#include <iostream>
#include <ctime>
#include <boost/pool/pool_alloc.hpp>
#define TEST_LIST_SIZE (10000000)
typedef boost::fast_pool_allocator<
int,
boost::default_user_allocator_new_delete,
boost::details::pool::default_mutex,
TEST_LIST_SIZE> BoostIntAllocator;
typedef boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(int)> BoostIntAllocatorPool;
void createMillionVector()
{
std::vector<int> v;
for (int i = 0; i < TEST_LIST_SIZE; ++i)
{
v.push_back(i);
}
}
void createPreallocatedMillionVector()
{
std::vector<int> v;
v.reserve(TEST_LIST_SIZE);
for (int i = 0; i < TEST_LIST_SIZE; ++i)
{
v.push_back(i);
}
}
void createMillionListBack()
{
std::list<int> L;
for (int i = 0; i < TEST_LIST_SIZE; ++i)
{
L.push_back(i);
}
}
void createMillionListFront()
{
std::list<int> L;
for (int i = 0; i < TEST_LIST_SIZE; ++i)
{
L.push_front(i);
}
}
void createPoolAllocatedMillionListFront()
{
std::list<int, BoostIntAllocator> L;
for (int i = 0; i < TEST_LIST_SIZE; ++i)
{
L.push_front(i);
}
BoostIntAllocatorPool::purge_memory();
}
int main()
{
clock_t preVectorTime = clock();
createMillionVector();
clock_t postVectorTime = clock();
createPreallocatedMillionVector();
clock_t postPreallocatedVectorTime = clock();
createMillionListBack();
clock_t postListBackTime = clock();
createMillionListFront();
clock_t postListFrontTime = clock();
createPoolAllocatedMillionListFront();
clock_t postPoolAllocatedListFrontTime = clock();
std::cout << "Vector took " << (postVectorTime - preVectorTime) / (float)CLOCKS_PER_SEC << " seconds." << std::endl;
std::cout << "Preallocated Vector took " << (postPreallocatedVectorTime - postVectorTime) / (float)CLOCKS_PER_SEC << " seconds." << std::endl;
std::cout << "Back pushed List took " << (postListBackTime - postPreallocatedVectorTime) / (float)CLOCKS_PER_SEC << " seconds." << std::endl;
std::cout << "Front pushed List took " << (postListFrontTime - postListBackTime) / (float)CLOCKS_PER_SEC << " seconds." << std::endl;
std::cout << "Front pushed Pool allocated list took " << (postPoolAllocatedListFrontTime - postListFrontTime) / (float)CLOCKS_PER_SEC << " seconds." << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment