Created
September 23, 2015 19:36
-
-
Save stoyannk/9df2df9d25e1acba7747 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <unordered_set> | |
#include <memory> | |
#include <chrono> | |
#include <vector> | |
#include <algorithm> | |
#include <cmath> | |
#include <iostream> | |
#include <sparsehash/dense_hash_set> | |
using ptr_t = std::shared_ptr<float>; | |
using ptr_set = std::unordered_set<ptr_t>; | |
using ptr_vec = std::vector<ptr_t>; | |
using ptr_gset = google::dense_hash_set<ptr_t, std::hash<ptr_t>>; | |
#define UNIQUE_VEC 0 | |
int main() | |
{ | |
static const auto COUNT = 10000u; | |
static const auto ITERATIONS = 100u; | |
static const auto REPEAT = 1u; | |
// static container with all allocated pointers | |
ptr_vec pointers; | |
pointers.reserve(COUNT); | |
for (auto i = 0u; i < COUNT / REPEAT; ++i) { | |
auto ptr = std::make_shared<float>(float(i)); | |
for (auto r = 0u; r < REPEAT; ++r) { | |
pointers.push_back(ptr); | |
} | |
} | |
std::random_shuffle(pointers.begin(), pointers.end()); | |
using namespace std::chrono; | |
// Unordered set | |
{ | |
auto before = high_resolution_clock::now(); | |
for (auto i = 0u; i < ITERATIONS; ++i) | |
{ | |
ptr_set testSet; | |
// fill set | |
for (auto& p : pointers) { | |
testSet.insert(p); | |
} | |
// simulate work | |
for (auto& p : testSet) { | |
*p = sqrtf(*p); | |
} | |
} | |
auto after = high_resolution_clock::now(); | |
const auto timeForSet = after - before; | |
std::cout << "Time for set: " << duration_cast<microseconds>(timeForSet).count() / 1000.f << std::endl; | |
} | |
// Vector | |
{ | |
auto before = high_resolution_clock::now(); | |
for (auto i = 0u; i < ITERATIONS; ++i) { | |
ptr_vec testVec; | |
for (auto& p : pointers) { | |
testVec.push_back(p); | |
} | |
#if UNIQUE_VEC | |
std::sort(testVec.begin(), testVec.end()); | |
testVec.erase(std::unique(testVec.begin(), testVec.end()), testVec.end()); | |
#endif | |
for (auto& p : testVec) { | |
*p = sqrtf(*p); | |
} | |
} | |
auto after = high_resolution_clock::now(); | |
const auto timeForSet = after - before; | |
std::cout << "Time for vector: " << duration_cast<microseconds>(timeForSet).count() / 1000.f << std::endl; | |
} | |
{ | |
auto before = high_resolution_clock::now(); | |
for (auto i = 0u; i < ITERATIONS; ++i) { | |
ptr_gset testGset; | |
testGset.set_empty_key(ptr_t()); | |
for (auto& p : pointers) { | |
testGset.insert(p); | |
} | |
for (auto& p : testGset) { | |
*p = sqrtf(*p); | |
} | |
} | |
auto after = high_resolution_clock::now(); | |
const auto timeForSet = after - before; | |
std::cout << "Time for google set: " << duration_cast<microseconds>(timeForSet).count() / 1000.f << std::endl; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment