#include <iostream>
#include <type_traits>
#include <memory>
#include <algorithm>
#include <unordered_map>
#include <chrono>
#include <limits>
#include "absl/container/flat_hash_map.h"
#include "absl/container/node_hash_map.h"
template< typename KeyT, typename ValueT>
using UnorderedMap = absl::flat_hash_map<KeyT, ValueT>;
//using UnorderedMap = std::unordered_map<KeyT, ValueT>;
//using UnorderedMap = std::conditional_t< (sizeof(ValueT) <= sizeof(std::unique_ptr<int>)),
// absl::flat_hash_map<KeyT, ValueT>, absl::node_hash_map<KeyT, ValueT> >;
// This is a marker to help located positions in assembly code, should I want to do that.
void __attribute__((noinline)) bookmark() {__asm__("");}
constexpr int InitialSize = 1024;
int main() {
UnorderedMap<unsigned int, unsigned int> myMap(InitialSize * .25);
unsigned int startingPoint(10000000); // some large arbitrary number so I don't start at 0.
unsigned int index(startingPoint);
std::cout << "my Bucket Count: " << myMap.bucket_count() << std::endl;
for( ; index < startingPoint + 10; ++index ) // Add 10 entries
{
myMap.emplace( index, index+1);
}
// Verify that the bucket count has not changed after inserting a few elements
std::cout << "my Bucket Count: " << myMap.bucket_count() << std::endl;
int subTotal = 0;
long int totalTime(std::chrono::duration<int>::zero().count());
long int highTime(std::chrono::duration<int>::zero().count());
long int lowTime(std::chrono::duration<int>::max().count());
std::vector<unsigned int> times;
std::cout << "Total Time: " << totalTime << std::endl;
// Here is the bulk of the work. We are going to remove an item and insert a new item so that
// a rehash shouldn't occur. And we are going to do it a large number of times.
// Along the way, let's clock how long an insert in the table takes.
for(; index < startingPoint + 200000; ++index )
{
myMap.erase(index-10); // remove an old entry
bookmark();
auto start = std::chrono::high_resolution_clock::now();
myMap.emplace(index, index - 10);
auto stop = std::chrono::high_resolution_clock::now();
bookmark();
// Now calc the the time and store it, before repeating
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop-start);
auto nanos = duration.count();
times.push_back(nanos);
totalTime += nanos;
if( nanos < lowTime)
lowTime = nanos;
else if( nanos > highTime )
highTime = nanos;
}
std::sort(times.begin(), times.end());
long int median(0);
if( times.size()%2 == 0 )
{
median = (times[times.size()/2] + times[times.size()/2 - 1]) / 2;
}
else
median = (times[(times.size()+1) / 2]);
std::cout << "my Bucket Count: " << myMap.bucket_count() << std::endl
<< "highTime " << highTime
<< " lowTime " << lowTime
<< " average " << totalTime/times.size()
<< " median " << median << std::endl;
std::cout << "size of myMap: " << myMap.size() << std::endl;
return 0;
}
--
You received this message because you are subscribed to the Google Groups "Abseil.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to abseil-io+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/abseil-io/9bbdd071-0468-412a-a9d4-1571b20d6975n%40googlegroups.com.
Vào Th 7, 22 thg 8, 2020 lúc 00:19 Mean Dog <meand...@gmail.com> đã viết:I've been playing around with absl flat hash map trying to compare it with std unordered map.I wrote a small program, included below. It's a simple program that adds 10 entries into a map. Then it enters a large loop where it erases the oldest entry and inserts a new entry. Each time the key is incremented by 1. At no time during the loop do I expect a rehash to occur.
I am using std::chrono for timing and invoking the program with "taskset -c 2"
To view this discussion on the web visit https://groups.google.com/d/msgid/abseil-io/CAOVuf7RFQeoGpVjNUW5LPZ82nxE_tU__Tu-uszR05VgCd%3DCRcg%40mail.gmail.com.