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

190 lines
5.6 KiB
C++
Raw Normal View History

2011-12-12 10:05:35 +00:00
#include <iostream>
#include <iomanip>
#include <vector>
2014-01-08 16:33:28 +00:00
#include <unordered_map>
2011-12-12 10:05:35 +00:00
#include <sparsehash/dense_hash_map>
#include <sparsehash/sparse_hash_map>
2011-12-19 02:00:40 +00:00
2015-10-05 00:44:40 +00:00
#include <DB/Common/Stopwatch.h>
2011-12-19 02:00:40 +00:00
//#define DBMS_HASH_MAP_COUNT_COLLISIONS
#define DBMS_HASH_MAP_DEBUG_RESIZES
2012-06-01 10:45:29 +00:00
2011-12-19 02:00:40 +00:00
#include <DB/Core/Types.h>
2012-06-01 10:45:29 +00:00
#include <DB/IO/ReadBufferFromFile.h>
#include <DB/IO/CompressedReadBuffer.h>
#include <DB/Common/HashTable/HashMap.h>
2011-12-12 10:05:35 +00:00
typedef UInt64 Key;
typedef UInt64 Value;
struct CellWithoutZeroWithSavedHash : public HashMapCell<Key, Value, DefaultHash<Key> >
{
// size_t saved_hash;
static constexpr bool need_zero_value_storage = false;
CellWithoutZeroWithSavedHash() : HashMapCell() {}
CellWithoutZeroWithSavedHash(const Key & key_, const State & state) : HashMapCell(key_, state) {}
CellWithoutZeroWithSavedHash(const value_type & value_, const State & state) : HashMapCell(value_, state) {}
/* bool keyEquals(const Key & key_) const { return value.first == key_; }
bool keyEquals(const CellWithoutZeroWithSavedHash & other) const { return saved_hash == other.saved_hash && value.first == other.value.first; }
void setHash(size_t hash_value) { saved_hash = hash_value; }
size_t getHash(const DefaultHash<Key> & hash) const { return saved_hash; }*/
};
2014-05-03 16:03:49 +00:00
struct Grower : public HashTableGrower<>
{
/// Состояние этой структуры достаточно, чтобы получить размер буфера хэш-таблицы.
/// Определяет начальный размер хэш-таблицы.
static const size_t initial_size_degree = 16;
Grower() { size_degree = initial_size_degree; }
// size_t max_fill = (1 << initial_size_degree) * 0.9;
/// Размер хэш-таблицы в ячейках.
size_t bufSize() const { return 1 << size_degree; }
size_t maxFill() const { return 1 << (size_degree - 1); }
// size_t maxFill() const { return max_fill; }
size_t mask() const { return bufSize() - 1; }
/// Из значения хэш-функции получить номер ячейки в хэш-таблице.
size_t place(size_t x) const { return x & mask(); }
/// Следующая ячейка в цепочке разрешения коллизий.
size_t next(size_t pos) const { ++pos; return pos & mask(); }
/// Является ли хэш-таблица достаточно заполненной. Нужно увеличить размер хэш-таблицы, или удалить из неё что-нибудь ненужное.
bool overflow(size_t elems) const { return elems > maxFill(); }
/// Увеличить размер хэш-таблицы.
void increaseSize()
{
size_degree += size_degree >= 23 ? 1 : 2;
// max_fill = (1 << size_degree) * 0.9;
}
/// Установить размер буфера по количеству элементов хэш-таблицы. Используется при десериализации хэш-таблицы.
void set(size_t num_elems)
{
throw Poco::Exception(__PRETTY_FUNCTION__);
}
};
2011-12-12 10:05:35 +00:00
int main(int argc, char ** argv)
{
2012-06-01 10:45:29 +00:00
size_t n = atoi(argv[1]);
size_t m = atoi(argv[2]);
2011-12-19 02:00:40 +00:00
std::vector<Key> data(n);
std::cerr << "sizeof(Key) = " << sizeof(Key) << ", sizeof(Value) = " << sizeof(Value) << std::endl;
{
2012-06-01 10:45:29 +00:00
Stopwatch watch;
DB::ReadBufferFromFileDescriptor in1(STDIN_FILENO);
2012-06-01 10:45:29 +00:00
DB::CompressedReadBuffer in2(in1);
in2.readStrict(reinterpret_cast<char*>(&data[0]), sizeof(data[0]) * n);
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
if (m == 1)
2011-12-12 10:05:35 +00:00
{
Stopwatch watch;
2011-12-19 02:00:40 +00:00
// typedef HashMap<Key, Value> Map;
/// Из-за WithoutZero быстрее на 0.7% (для не влезающей в L3-кэш) - 2.3% (для влезающей в L3-кэш).
typedef HashMapTable<Key, CellWithoutZeroWithSavedHash, DefaultHash<Key>, Grower> Map;
Map map;
Map::iterator it;
2011-12-19 02:00:40 +00:00
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)
2012-06-01 10:45:29 +00:00
it->second = 0;
++it->second;
2011-12-12 10:05:35 +00:00
}
watch.stop();
std::cerr << std::fixed << std::setprecision(2)
<< "HashMap. Size: " << map.size()
2011-12-12 10:05:35 +00:00
<< ", elapsed: " << watch.elapsedSeconds()
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
2012-06-01 10:45:29 +00:00
<< ", collisions: " << map.getCollisions()
#endif
2011-12-12 10:05:35 +00:00
<< std::endl;
}
if (m == 2)
2011-12-19 02:00:40 +00:00
{
2011-12-12 10:05:35 +00:00
Stopwatch watch;
std::unordered_map<Key, Value, DefaultHash<Key> > map;
2011-12-12 10:05:35 +00:00
for (size_t i = 0; i < n; ++i)
2012-06-01 10:45:29 +00:00
++map[data[i]];
2011-12-12 10:05:35 +00:00
watch.stop();
std::cerr << std::fixed << std::setprecision(2)
2014-01-08 16:33:28 +00:00
<< "std::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
if (m == 3)
2011-12-12 10:05:35 +00:00
{
Stopwatch watch;
google::dense_hash_map<Key, Value, DefaultHash<Key> > map;
2012-06-01 10:45:29 +00:00
map.set_empty_key(-1ULL);
2011-12-12 10:05:35 +00:00
for (size_t i = 0; i < n; ++i)
2012-06-01 10:45:29 +00:00
++map[data[i]];
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;
}
if (m == 4)
2012-06-01 10:45:29 +00:00
{
2011-12-12 10:05:35 +00:00
Stopwatch watch;
google::sparse_hash_map<Key, Value, DefaultHash<Key> > map;
2011-12-12 10:05:35 +00:00
for (size_t i = 0; i < n; ++i)
2012-06-01 10:45:29 +00:00
++map[data[i]];
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;
2012-06-01 10:45:29 +00:00
}
2011-12-12 10:05:35 +00:00
return 0;
}