ClickHouse/dbms/src/Interpreters/tests/hash_map2.cpp

191 lines
4.3 KiB
C++
Raw Normal View History

2011-12-12 10:05:35 +00:00
#include <iostream>
#include <iomanip>
#include <vector>
#include <tr1/unordered_map>
#include <city.h>
#include <statdaemons/Stopwatch.h>
#include <DB/Core/Types.h>
#include <DB/IO/ReadBufferFromFileDescriptor.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/Interpreters/HashMap.h>
2011-12-19 02:00:40 +00:00
#include <iostream>
#include <iomanip>
#include <vector>
#include <tr1/unordered_map>
#include <google/dense_hash_map>
#include <google/sparse_hash_map>
#include <statdaemons/Stopwatch.h>
#include <DB/Core/Types.h>
#include <DB/Interpreters/HashMap.h>
#include <DB/AggregateFunctions/IAggregateFunction.h>
#include <DB/AggregateFunctions/AggregateFunctionFactory.h>
#include <DB/DataTypes/DataTypesNumberFixed.h>
2011-12-19 04:26:27 +00:00
struct SimpleString
2011-12-12 10:05:35 +00:00
{
2011-12-19 04:26:27 +00:00
char * data;
2011-12-19 02:00:40 +00:00
2011-12-19 04:26:27 +00:00
SimpleString() : data(NULL) {}
SimpleString(const std::string & s)
{
data = reinterpret_cast<char *>(malloc(s.size()));
memcpy(data, s.data(), s.size());
}
SimpleString(const SimpleString & s)
{
/// move
data = s.data;
const_cast<char *&>(s.data) = NULL;
}
~SimpleString()
{
free(data);
}
2011-12-12 10:05:35 +00:00
2011-12-19 04:26:27 +00:00
bool operator== (const SimpleString & s)
{
return 0 == strcmp(data, s.data);
}
2011-12-19 02:00:40 +00:00
2011-12-19 04:26:27 +00:00
bool operator!= (const SimpleString & s)
{
return !operator==(s);
}
};
struct SimpleStringZeroTraits
{
static inline bool check(const SimpleString & x) { return 0 == x.data; }
static inline void set(SimpleString & x) { x.data = 0; }
};
2011-12-12 10:05:35 +00:00
2011-12-19 04:26:27 +00:00
struct SimpleStringHash
2011-12-12 10:05:35 +00:00
{
2011-12-19 04:26:27 +00:00
size_t operator()(const SimpleString & x) const { return CityHash64(x.data, strlen(x.data)); }
2011-12-12 10:05:35 +00:00
};
int main(int argc, char ** argv)
{
2011-12-19 02:00:40 +00:00
typedef DB::String Key;
typedef DB::AggregateFunctions Value;
DB::AggregateFunctionFactory factory;
DB::DataTypes data_types_empty;
DB::DataTypes data_types_uint64;
data_types_uint64.push_back(new DB::DataTypeUInt64);
2011-12-19 04:26:27 +00:00
size_t n = 1000000;
2011-12-19 02:00:40 +00:00
std::vector<Key> data(n);
Value value;
value.push_back(factory.get("count", data_types_empty));
value.push_back(factory.get("avg", data_types_uint64));
value.push_back(factory.get("uniq", data_types_uint64));
std::cerr << "sizeof(Key) = " << sizeof(Key) << ", sizeof(Value) = " << sizeof(Value) << std::endl;
{
DB::ReadBufferFromFileDescriptor buf(0);
Stopwatch watch;
for (size_t i = 0; !buf.eof(); ++i)
{
DB::readEscapedString(data[i], buf);
DB::assertString("\n", buf);
}
watch.stop();
std::cerr << std::fixed << std::setprecision(2)
<< "Vector. Size: " << n
<< ", elapsed: " << watch.elapsedSeconds()
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
<< std::endl;
}
2011-12-12 10:05:35 +00:00
{
Stopwatch watch;
2011-12-19 02:00:40 +00:00
2011-12-19 04:26:27 +00:00
typedef DB::HashMap<SimpleString, Value, SimpleStringHash, SimpleStringZeroTraits> Map;
2011-12-19 02:00:40 +00:00
Map map;
Map::iterator it;
bool inserted;
for (size_t i = 0; i < n; ++i)
2011-12-12 10:05:35 +00:00
{
2011-12-19 02:00:40 +00:00
map.emplace(data[i], it, inserted);
if (inserted)
new(&it->second) Value(value);
2011-12-12 10:05:35 +00:00
}
watch.stop();
std::cerr << std::fixed << std::setprecision(2)
2011-12-19 02:00:40 +00:00
<< "DB::HashMap. Size: " << map.size()
2011-12-12 10:05:35 +00:00
<< ", elapsed: " << watch.elapsedSeconds()
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
<< std::endl;
}
2011-12-19 02:00:40 +00:00
{
2011-12-12 10:05:35 +00:00
Stopwatch watch;
2011-12-19 02:00:40 +00:00
std::tr1::unordered_map<Key, Value> map;
2011-12-12 10:05:35 +00:00
for (size_t i = 0; i < n; ++i)
2011-12-19 02:00:40 +00:00
map.insert(std::make_pair(data[i], value));
2011-12-12 10:05:35 +00:00
watch.stop();
std::cerr << std::fixed << std::setprecision(2)
2011-12-19 02:00:40 +00:00
<< "std::tr1::unordered_map. Size: " << map.size()
2011-12-12 10:05:35 +00:00
<< ", elapsed: " << watch.elapsedSeconds()
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
<< std::endl;
2011-12-19 02:00:40 +00:00
}
2011-12-12 10:05:35 +00:00
{
Stopwatch watch;
2011-12-19 02:00:40 +00:00
google::dense_hash_map<Key, Value> map;
map.set_empty_key("");
2011-12-12 10:05:35 +00:00
for (size_t i = 0; i < n; ++i)
2011-12-19 02:00:40 +00:00
map.insert(std::make_pair(data[i], value));
2011-12-12 10:05:35 +00:00
watch.stop();
std::cerr << std::fixed << std::setprecision(2)
2011-12-19 02:00:40 +00:00
<< "google::dense_hash_map. Size: " << map.size()
2011-12-12 10:05:35 +00:00
<< ", elapsed: " << watch.elapsedSeconds()
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
<< std::endl;
}
/*{
Stopwatch watch;
2011-12-19 02:00:40 +00:00
google::sparse_hash_map<Key, Value> map;
2011-12-12 10:05:35 +00:00
for (size_t i = 0; i < n; ++i)
2011-12-19 02:00:40 +00:00
map.insert(std::make_pair(data[i], value));
2011-12-12 10:05:35 +00:00
watch.stop();
std::cerr << std::fixed << std::setprecision(2)
2011-12-19 02:00:40 +00:00
<< "google::sparse_hash_map. Size: " << map.size()
2011-12-12 10:05:35 +00:00
<< ", elapsed: " << watch.elapsedSeconds()
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
<< std::endl;
}*/
return 0;
}
2011-12-19 02:00:40 +00:00