#include <hash_map>
#include <map>
#include <stdio.h>
#include <ctime>
#include <iostream>
enum {
TEST_TYPE_SIZE = 6,
TEST_DATASET_SIZE = 5000000,
EXPERIMENT_SIZE= 1,
};
struct some_type {
enum {SIZE = TEST_TYPE_SIZE};
int data[SIZE];
int tag_value;
};
typedef std::map<char*, some_type> map_t;
typedef std::hash_map<char*, some_type> hashmap_t;
int main()
{
std::list<some_type*> test_data;
std::list<char*> trashcan;
typedef std::list<char*>::iterator tit;
for (size_t i = 0; i < TEST_DATASET_SIZE; ++i) {
test_data.push_back(new some_type);
trashcan.push_back(new char[rand()%100]);
}
//////////////////////////////////////////////////////////////////////////
// map::insert
map_t test_maps[EXPERIMENT_SIZE];
std::clock_t start = std::clock();
for(size_t i = 0; i < EXPERIMENT_SIZE; ++i)
{
map_t& m = test_maps[i];
for(tit it = trashcan.begin(); it != trashcan.end(); ++it) {
some_type v;
v.tag_value = rand();
*(int*)v.data = v.tag_value;
m.insert(std::make_pair(*it, v));
}
}
std::clock_t end = std::clock();
size_t dummy_val = 0;
for(size_t i = 0; i < EXPERIMENT_SIZE; ++i) {
map_t& m = test_maps[i];
typedef map_t::iterator iter;
for( iter it = m.begin(); it != m.end(); ++it) {
dummy_val += it->second.data[1];
}
}
std::clock_t map_insert_result = (end - start);
std::cout << "std::map insert score: " << map_insert_result << " (" << dummy_val << ")" << std::endl;
//////////////////////////////////////////////////////////////////////////
hashmap_t test_hashmaps[EXPERIMENT_SIZE];
start = std::clock();
for(size_t i = 0; i < EXPERIMENT_SIZE; ++i)
{
hashmap_t& m = test_hashmaps[i];
for(tit it = trashcan.begin(); it != trashcan.end(); ++it) {
some_type v;
v.tag_value = rand();
*(int*)v.data = v.tag_value;
m.insert(std::make_pair(*it, v));
}
}
end = std::clock();
dummy_val = 0;
for(size_t i = 0; i < EXPERIMENT_SIZE; ++i) {
hashmap_t& m = test_hashmaps[i];
typedef hashmap_t::iterator iter;
for( iter it = m.begin(); it != m.end(); ++it) {
dummy_val += it->second.data[1];
}
}
std::clock_t hashmap_insert_result = (end - start);
std::cout << "std::hash_map insert score: " << hashmap_insert_result << " (" << dummy_val << ")" << std::endl;
//////////////////////////////////////////////////////////////////////////
dummy_val = 0;
start = std::clock();
for(tit it = trashcan.begin(); it != trashcan.end(); ++it) {
for(size_t i = 0; i < EXPERIMENT_SIZE; ++i)
{
map_t& m = test_maps[i];
map_t::iterator res = m.find(*it);
if (res != m.end())
dummy_val += res->second.data[3];
}
}
end = std::clock();
std::clock_t map_search_result = (end - start);
std::cout << "std::map search score: " << map_search_result << " (" << dummy_val << ")" << std::endl;
//////////////////////////////////////////////////////////////////////////
dummy_val = 0;
start = std::clock();
for(tit it = trashcan.begin(); it != trashcan.end(); ++it) {
for(size_t i = 0; i < EXPERIMENT_SIZE; ++i)
{
hashmap_t& m = test_hashmaps[i];
hashmap_t::iterator res = m.find(*it);
if (res != m.end())
dummy_val += res->second.data[3];
}
}
end = std::clock();
std::clock_t hashmap_search_result = (end - start);
std::cout << "std::hash_map search score: " << hashmap_search_result << " (" << dummy_val << ")" << std::endl;
return 0;
}