forked from MassiveAtoms/hashmap-bench
310 lines
10 KiB
C++
310 lines
10 KiB
C++
|
|
#ifndef TESTS_H
|
|
#define TESTS_H
|
|
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <iterator>
|
|
#include <chrono>
|
|
|
|
#include "./generator.h"
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
template <class T>
|
|
nanoseconds insert_int_test(int size, T testmap){
|
|
// init hashmap, insert size - 10k items
|
|
testmap.reserve(size);
|
|
for (int i = 0; i < size - 10000; ++i){
|
|
int a = gen_int();
|
|
testmap.insert({a,a});
|
|
}
|
|
// generate 10k keys
|
|
std::vector<int> keys(10000);
|
|
std::generate(keys.begin(), keys.end(), gen_int);
|
|
|
|
// benchmark vector access time
|
|
time_point<steady_clock> start_v_access = steady_clock::now();
|
|
for (auto i : keys){
|
|
if (i == -1) { // it'll never be, this is just anti optimisation
|
|
std::cout << "Something is very wrong!";
|
|
}
|
|
|
|
}
|
|
time_point<steady_clock> end_v_access = steady_clock::now();
|
|
auto vector_time = duration_cast<nanoseconds>(end_v_access - start_v_access);
|
|
|
|
// measure insert time
|
|
time_point<steady_clock> start_insert_test = steady_clock::now();
|
|
for (auto i : keys){
|
|
testmap.insert({i,i});
|
|
}
|
|
time_point<steady_clock> end_insert_test = steady_clock::now();
|
|
|
|
// time per insert
|
|
auto duration = duration_cast<nanoseconds>(end_insert_test - start_insert_test) - vector_time ;
|
|
return duration / 10000;
|
|
|
|
}
|
|
|
|
|
|
template <class T>
|
|
nanoseconds insert_string_test(int size, T testmap){
|
|
// init hashmap, insert size - 10k items
|
|
testmap.reserve(size);
|
|
for (int i = 0; i < size - 10000; ++i){
|
|
std::string temp = gen_string();
|
|
testmap.insert({temp,temp});
|
|
}
|
|
// generate 10k keys
|
|
std::vector<std::string> keys(10000);
|
|
std::generate(keys.begin(), keys.end(), gen_string);
|
|
|
|
// benchmark vector access time
|
|
time_point<steady_clock> start_v_access = steady_clock::now();
|
|
for (auto i : keys){
|
|
if (i == "a") { // it'll never be, this is just anti optimisation
|
|
std::cout << "Something is very wrong!";
|
|
}
|
|
|
|
}
|
|
time_point<steady_clock> end_v_access = steady_clock::now();
|
|
auto vector_time = duration_cast<nanoseconds>(end_v_access - start_v_access);
|
|
|
|
// measure insert time
|
|
time_point<steady_clock> start_insert_test = steady_clock::now();
|
|
for (auto i : keys){
|
|
testmap.insert({i,i});
|
|
}
|
|
time_point<steady_clock> end_insert_test = steady_clock::now();
|
|
|
|
// time per insert
|
|
auto duration = duration_cast<nanoseconds>(end_insert_test - start_insert_test) - vector_time ;
|
|
return duration / 10000;
|
|
|
|
}
|
|
|
|
template<class T>
|
|
nanoseconds lookup_int_test(int size, T testmap){
|
|
// reserve, get random 10k keys that are inserted, insert keys
|
|
testmap.reserve(size);
|
|
std::vector<int> sample_inserted;
|
|
sample_inserted.reserve(10000);
|
|
|
|
|
|
{
|
|
std::vector<int> keys(size);
|
|
std::generate(keys.begin(), keys.end(), gen_int);
|
|
std::sample(keys.begin(), keys.end(), std::back_inserter(sample_inserted) , 10000, generator);
|
|
for (auto i : keys){
|
|
testmap.insert({i, i});
|
|
}
|
|
}
|
|
// benchmark vector access time
|
|
time_point<steady_clock> start_v_access = steady_clock::now();
|
|
for (auto i : sample_inserted){
|
|
if (i == -1) { // it'll never be, this is just anti optimisation
|
|
std::cout << "Something is very wrong!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_v_access = steady_clock::now();
|
|
auto vector_time = duration_cast<nanoseconds>(end_v_access - start_v_access);
|
|
|
|
// benchmark access time of hashmap
|
|
time_point<steady_clock> start_benchmark = steady_clock::now();
|
|
for (auto i : sample_inserted){
|
|
if (testmap[i] == -1){
|
|
std::cout << "SOMETHUNG IS WRONG!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_benchmark = steady_clock::now();
|
|
auto duration = duration_cast<nanoseconds>(end_benchmark - start_benchmark) - vector_time;
|
|
return duration / 10000;
|
|
}
|
|
|
|
|
|
template<class T>
|
|
nanoseconds lookup_string_test(int size, T testmap){
|
|
// reserve, get random 10k keys that are inserted, insert keys
|
|
testmap.reserve(size);
|
|
std::vector<std::string> sample_inserted(1000);
|
|
// sample_inserted.reserve(10000);
|
|
{
|
|
std::vector<std::string> keys(size);
|
|
std::generate(keys.begin(), keys.end(), gen_string);
|
|
std::sample(keys.begin(), keys.end(), std::back_inserter(sample_inserted) , 10000, generator);
|
|
for (auto i : keys){
|
|
testmap.insert({i, i});
|
|
}
|
|
}
|
|
// benchmark vector access time
|
|
time_point<steady_clock> start_v_access = steady_clock::now();
|
|
for (auto i : sample_inserted){
|
|
if (i == "a") { // it'll never be, this is just anti optimisation
|
|
std::cout << "Something is very wrong!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_v_access = steady_clock::now();
|
|
auto vector_time = duration_cast<nanoseconds>(end_v_access - start_v_access);
|
|
|
|
// benchmark access time of hashmap
|
|
time_point<steady_clock> start_benchmark = steady_clock::now();
|
|
|
|
for (auto i : sample_inserted){
|
|
if (testmap[i] == "a"){
|
|
std::cout << "SOMETHUNG IS WRONG!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_benchmark = steady_clock::now();
|
|
auto duration = duration_cast<nanoseconds>(end_benchmark - start_benchmark) - vector_time;
|
|
return duration / 10000;
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
nanoseconds nolookup_int_test(int size, T testmap){
|
|
// reserve, get random 10k keys that are inserted, insert keys
|
|
testmap.reserve(size);
|
|
std::vector<int>lookup_keys(10000);
|
|
std::generate(lookup_keys.begin(), lookup_keys.end(), gen_unsuccesfull_int);
|
|
for (int i = 0; i < size; ++i){
|
|
int temp = gen_int();
|
|
testmap.insert({temp, temp});
|
|
}
|
|
|
|
// benchmark vector access time
|
|
time_point<steady_clock> start_v_access = steady_clock::now();
|
|
for (auto i : lookup_keys){
|
|
if (i == -1) { // it'll never be, this is just anti optimisation
|
|
std::cout << "Something is very wrong!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_v_access = steady_clock::now();
|
|
auto vector_time = duration_cast<nanoseconds>(end_v_access - start_v_access);
|
|
|
|
|
|
// benchmark access time of hashmap
|
|
time_point<steady_clock> start_benchmark = steady_clock::now();
|
|
for (auto i : lookup_keys){
|
|
if (testmap[i] == -1){
|
|
std::cout << "SOMETHUNG IS WRONG!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_benchmark = steady_clock::now();
|
|
auto duration = duration_cast<nanoseconds>(end_benchmark - start_benchmark) - vector_time;
|
|
return duration / 10000;
|
|
}
|
|
|
|
|
|
template<class T>
|
|
nanoseconds nolookup_string_test(int size, T testmap){
|
|
// reserve, get random 10k keys that are inserted, insert keys
|
|
testmap.reserve(size);
|
|
std::vector<std::string>lookup_keys(10000);
|
|
std::generate(lookup_keys.begin(), lookup_keys.end(), gen_unsuccesfull_string);
|
|
for (int i = 0; i < size; ++i){
|
|
std::string temp = gen_string();
|
|
testmap.insert({temp, temp});
|
|
}
|
|
|
|
// benchmark vector access time
|
|
time_point<steady_clock> start_v_access = steady_clock::now();
|
|
for (auto i : lookup_keys){
|
|
if (i == "a") { // it'll never be, this is just anti optimisation
|
|
std::cout << "Something is very wrong!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_v_access = steady_clock::now();
|
|
auto vector_time = duration_cast<nanoseconds>(end_v_access - start_v_access);
|
|
|
|
|
|
// benchmark access time of hashmap
|
|
time_point<steady_clock> start_benchmark = steady_clock::now();
|
|
for (auto i : lookup_keys){
|
|
if (testmap[i] == "a"){
|
|
std::cout << "SOMETHUNG IS WRONG!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_benchmark = steady_clock::now();
|
|
auto duration = duration_cast<nanoseconds>(end_benchmark - start_benchmark) - vector_time;
|
|
return duration / 10000;
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
nanoseconds delete_int_test(int size, T testmap){
|
|
// reserve, get random 10k keys that are inserted, insert keys
|
|
testmap.reserve(size);
|
|
std::vector<int> sample_inserted;
|
|
sample_inserted.reserve(10000);
|
|
|
|
{
|
|
std::vector<int> keys(size);
|
|
std::generate(keys.begin(), keys.end(), gen_int);
|
|
std::sample(keys.begin(), keys.end(), std::back_inserter(sample_inserted) , 10000, generator);
|
|
for (auto i : keys){
|
|
testmap.insert({i, i});
|
|
}
|
|
}
|
|
// benchmark vector access time
|
|
time_point<steady_clock> start_v_access = steady_clock::now();
|
|
for (auto i : sample_inserted){
|
|
if (i == -1) { // it'll never be, this is just anti optimisation
|
|
std::cout << "Something is very wrong!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_v_access = steady_clock::now();
|
|
auto vector_time = duration_cast<nanoseconds>(end_v_access - start_v_access);
|
|
|
|
// benchmark access time of hashmap
|
|
time_point<steady_clock> start_benchmark = steady_clock::now();
|
|
for (auto i : sample_inserted){
|
|
testmap.erase(i);
|
|
}
|
|
time_point<steady_clock> end_benchmark = steady_clock::now();
|
|
auto duration = duration_cast<nanoseconds>(end_benchmark - start_benchmark) - vector_time;
|
|
return duration / 10000;
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
nanoseconds delete_string_test(int size, T testmap){
|
|
// reserve, get random 10k keys that are inserted, insert keys
|
|
testmap.reserve(size);
|
|
std::vector<std::string> sample_inserted;
|
|
sample_inserted.reserve(10000);
|
|
|
|
{
|
|
std::vector<std::string> keys(size);
|
|
std::generate(keys.begin(), keys.end(), gen_string);
|
|
std::sample(keys.begin(), keys.end(), std::back_inserter(sample_inserted) , 10000, generator);
|
|
for (auto i : keys){
|
|
testmap.insert({i, i});
|
|
}
|
|
}
|
|
// benchmark vector access time
|
|
time_point<steady_clock> start_v_access = steady_clock::now();
|
|
for (auto i : sample_inserted){
|
|
if (i == "a") { // it'll never be, this is just anti optimisation
|
|
std::cout << "Something is very wrong!";
|
|
}
|
|
}
|
|
time_point<steady_clock> end_v_access = steady_clock::now();
|
|
auto vector_time = duration_cast<nanoseconds>(end_v_access - start_v_access);
|
|
|
|
// benchmark access time of hashmap
|
|
time_point<steady_clock> start_benchmark = steady_clock::now();
|
|
for (auto i : sample_inserted){
|
|
testmap.erase(i);
|
|
}
|
|
time_point<steady_clock> end_benchmark = steady_clock::now();
|
|
auto duration = duration_cast<nanoseconds>(end_benchmark - start_benchmark) - vector_time;
|
|
return duration / 10000;
|
|
}
|
|
|
|
#endif /* TESTS_H */ |