Skip to content

Instantly share code, notes, and snippets.

@Ivesvdf
Created August 17, 2011 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ivesvdf/1152593 to your computer and use it in GitHub Desktop.
Save Ivesvdf/1152593 to your computer and use it in GitHub Desktop.
A performance comparison of linear access on std::list and std::vector
#include <list>
#include <vector>
#include <cmath>
#include <iostream>
#include "univhac.h"
int pow(int num, int exp)
{
int rv = 1;
for(int i = 0; i < exp; i++)
{
rv *= num;
}
return rv;
}
template<typename T>
void g(T)
{
// do nothing
}
template<typename STORAGE>
void test()
{
Univhac timer;
for(size_t arraySizeExp = 0; arraySizeExp < 18; arraySizeExp++)
{
const int size = pow(2, arraySizeExp);
timer.reset();
STORAGE stor(size);
const int factor = 5000;
for(int j = 0; j < factor; j++)
{
for(typename STORAGE::iterator it = stor.begin();
it != stor.end();
++it)
{
g<typename STORAGE::value_type>(*it);
}
}
std::cout << arraySizeExp << " " << timer.poll()/factor << std::endl;
}
}
int main(int argc, char **argv)
{
test<std::list<int> >();
test<std::vector<int> >();
test<std::list<int> >();
test<std::vector<int> >();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment