<http://www.gnu.org/licenses/>
#include <string>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <assert.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <map>
#include <ext/hash_map>
#include <tr1/unordered_map>
#include <stx/btree_multimap.h>
#include <vector>
#include <deque>
#include "memprofile.h"
const unsigned int insertnum = 1024000 * 8;
const int randseed = 34234235;
inline double timestamp()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec * 0.000001;
}
template <typename MapType>
struct Test_Map_Insert
{
Test_Map_Insert(unsigned int) {}
inline void run(unsigned int items)
{
MapType map;
srand(randseed);
for(unsigned int i = 0; i < items; i++) {
unsigned int r = rand();
map.insert( std::make_pair(r,r) );
}
assert( map.size() == items );
}
};
template < template<typename MapType> class TestClass >
struct TestFactory_Map
{
typedef TestClass< std::multimap<unsigned int, unsigned int> > StdMap;
typedef TestClass< __gnu_cxx::hash_multimap<unsigned int, unsigned int> > HashMap;
typedef TestClass< std::tr1::unordered_multimap<unsigned int, unsigned int> > UnorderedMap;
typedef TestClass< stx::btree_multimap<unsigned int, unsigned int,
std::less<unsigned int> > > BtreeMap;
};
template <typename ArrayType>
struct Test_Array_Insert
{
Test_Array_Insert(unsigned int) {}
inline void run(unsigned int items)
{
ArrayType array;
srand(randseed);
for(unsigned int i = 0; i < items; i++) {
unsigned int r = rand();
array.push_back( std::make_pair(r,r) );
}
assert( array.size() == items );
}
};
template < template<typename ArrayType> class TestClass >
struct TestFactory_Array
{
typedef TestClass< std::vector< std::pair<unsigned int, unsigned int> > > StdVector;
typedef TestClass< std::deque< std::pair<unsigned int, unsigned int> > > StdDeque;
};
template <typename TestClass>
void write_memprofile(const char* filename)
{
pid_t pid = fork();
if (pid == 0)
{
std::cout << "Writing memory profile " << filename << std::endl;
{
MemProfile mp(filename, 0.1, 16*1024);
TestClass test(insertnum);
double ts1 = timestamp();
test.run(insertnum);
double ts2 = timestamp();
std::cout << "done, time=" << (ts2-ts1) << std::endl;
}
exit(0);
}
int status;
wait(&status);
}
int main()
{
typedef TestFactory_Map<Test_Map_Insert> testmap_type;
write_memprofile< testmap_type::StdMap >("memprofile-stdmap.txt");
write_memprofile< testmap_type::HashMap >("memprofile-hashmap.txt");
write_memprofile< testmap_type::UnorderedMap >("memprofile-unorderedmap.txt");
write_memprofile< testmap_type::BtreeMap >("memprofile-btreemap.txt");
typedef TestFactory_Array<Test_Array_Insert> testarray_type;
write_memprofile< testarray_type::StdVector >("memprofile-vector.txt");
write_memprofile< testarray_type::StdDeque >("memprofile-deque.txt");
return 0;
}