2014-05-02 12:49:39 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include <sparsehash/dense_hash_map>
|
|
|
|
#include <sparsehash/sparse_hash_map>
|
|
|
|
|
|
|
|
//#define DBMS_HASH_MAP_DEBUG_RESIZES
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
|
|
|
#include <AggregateFunctions/UniquesHashSet.h>
|
2014-05-10 20:47:12 +00:00
|
|
|
|
2020-09-15 09:55:57 +00:00
|
|
|
#include <common/types.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
2018-12-28 18:15:26 +00:00
|
|
|
#include <Compression/CompressedReadBuffer.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/HashTable/TwoLevelHashTable.h>
|
|
|
|
#include <Common/HashTable/HashMap.h>
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
using Key = UInt64;
|
|
|
|
using Value = UInt64;
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2017-12-02 02:47:12 +00:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: program n\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:27:32 +00:00
|
|
|
size_t n = std::stol(argv[1]);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
std::vector<Key> data(n);
|
|
|
|
|
|
|
|
std::cerr << "sizeof(Key) = " << sizeof(Key) << ", sizeof(Value) = " << sizeof(Value) << std::endl;
|
|
|
|
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
DB::ReadBufferFromFileDescriptor in1(STDIN_FILENO);
|
|
|
|
DB::CompressedReadBuffer in2(in1);
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
in2.readStrict(reinterpret_cast<char*>(data.data()), sizeof(data[0]) * n);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
std::cerr << std::fixed << std::setprecision(2)
|
|
|
|
<< "Vector. Size: " << n
|
|
|
|
<< ", elapsed: " << watch.elapsedSeconds()
|
|
|
|
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
|
2017-08-30 18:13:32 +00:00
|
|
|
std::cerr << sizeof(HashMapCell<Key, Value, DefaultHash<Key>>) << std::endl;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-30 18:13:32 +00:00
|
|
|
using Map = TwoLevelHashTable<Key, HashMapCell<Key, Value, DefaultHash<Key>>, DefaultHash<Key>, HashTableGrower<8>, HashTableAllocator>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
Map map;
|
2019-08-20 09:58:44 +00:00
|
|
|
Map::LookupResult it;
|
2017-04-01 07:20:54 +00:00
|
|
|
bool inserted;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
map.emplace(data[i], it, inserted);
|
|
|
|
if (inserted)
|
2019-10-29 15:16:51 +00:00
|
|
|
it->getMapped() = 0;
|
|
|
|
++it->getMapped();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
std::cerr << std::fixed << std::setprecision(2)
|
|
|
|
<< "HashMap. Size: " << map.size()
|
|
|
|
<< ", elapsed: " << watch.elapsedSeconds()
|
|
|
|
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
size_t sum_counts = 0;
|
|
|
|
size_t elems = 0;
|
|
|
|
for (const auto & kv : map)
|
|
|
|
{
|
2019-10-29 15:16:51 +00:00
|
|
|
sum_counts += kv.getMapped();
|
2017-04-01 07:20:54 +00:00
|
|
|
++elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "sum_counts: " << sum_counts << ", elems: " << elems << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
|
2017-08-30 18:13:32 +00:00
|
|
|
using Map = TwoLevelHashTable<Key, HashMapCell<Key, Value, DefaultHash<Key>>, DefaultHash<Key>, HashTableGrower<8>, HashTableAllocator>;
|
2017-04-01 07:20:54 +00:00
|
|
|
//using Map = HashMap<Key, Value, UniquesHashSetDefaultHash>;
|
|
|
|
|
|
|
|
Map map;
|
2019-08-20 09:58:44 +00:00
|
|
|
Map::LookupResult it;
|
2017-04-01 07:20:54 +00:00
|
|
|
bool inserted;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
map.emplace(i, it, inserted);
|
|
|
|
if (inserted)
|
2019-10-29 15:16:51 +00:00
|
|
|
it->getMapped() = 0;
|
|
|
|
++it->getMapped();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
std::cerr << std::fixed << std::setprecision(2)
|
|
|
|
<< "HashMap. Size: " << map.size()
|
|
|
|
<< ", elapsed: " << watch.elapsedSeconds()
|
|
|
|
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
size_t sum_counts = 0;
|
|
|
|
size_t elems = 0;
|
|
|
|
for (const auto & kv : map)
|
|
|
|
{
|
2019-10-29 15:16:51 +00:00
|
|
|
sum_counts += kv.getMapped();
|
2017-04-01 07:20:54 +00:00
|
|
|
++elems;
|
|
|
|
|
2019-10-29 15:16:51 +00:00
|
|
|
if (kv.getKey() > n)
|
|
|
|
std::cerr << kv.getKey() << std::endl;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "sum_counts: " << sum_counts << ", elems: " << elems << std::endl;
|
|
|
|
|
|
|
|
if (sum_counts != n)
|
2020-10-24 21:50:52 +00:00
|
|
|
std::cerr << "Error!" << std::endl;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-05-02 12:49:39 +00:00
|
|
|
}
|