Last active
May 18, 2018 12:22
-
-
Save pudov/b158af49f449209b1be2 to your computer and use it in GitHub Desktop.
simple c++ containers performance benchmark
This file contains hidden or 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 <cstdint> | |
| #include <cassert> | |
| #include <typeinfo> | |
| #include <algorithm> | |
| #include <iterator> | |
| #include <iostream> | |
| #include <iomanip> | |
| #include <chrono> | |
| #include <ratio> | |
| #include <array> | |
| #include <vector> | |
| #include <set> | |
| #include <deque> | |
| #include <unordered_set> | |
| #include <boost/format.hpp> | |
| #include <boost/container/vector.hpp> | |
| #include <boost/container/flat_set.hpp> | |
| constexpr size_t SIZE = 2000000; | |
| constexpr size_t ELEM = 36546; | |
| using EL = uint32_t; | |
| template< | |
| typename Cont | |
| > | |
| Cont init() | |
| { | |
| Cont cont; | |
| for (size_t i = 0; i < SIZE; ++i) { | |
| cont.insert(cont.end(), typename Cont::value_type(i)); | |
| } | |
| return cont; | |
| } | |
| template<> | |
| std::array<EL, SIZE> init() | |
| { | |
| using Cont = std::array<EL, SIZE>; | |
| Cont cont; | |
| for (size_t i = 0; i < SIZE; ++i) { | |
| cont[i] = Cont::value_type(i); | |
| } | |
| return cont; | |
| } | |
| template<typename Cont> | |
| auto do_find(const Cont& c) -> decltype(c.end()) | |
| { | |
| return std::find(std::begin(c), std::end(c), ELEM); | |
| } | |
| template<> | |
| auto do_find(const std::set<EL>& c) -> decltype(c.end()) | |
| { | |
| return c.find(ELEM); | |
| } | |
| template<> | |
| auto do_find(const std::unordered_set<EL>& c) -> decltype(c.end()) | |
| { | |
| return c.find(ELEM); | |
| } | |
| template<> | |
| auto do_find(const boost::container::flat_set<EL>& c) -> decltype(c.end()) | |
| { | |
| return c.find(ELEM); | |
| } | |
| template<typename Cont> | |
| void benchmark() | |
| { | |
| auto cont = init<Cont>(); | |
| using clock = std::chrono::high_resolution_clock; | |
| auto start = clock::now(); | |
| typename Cont::value_type sum = 0; | |
| for (auto e : cont) { | |
| sum += e; | |
| } | |
| auto iter_dur = std::chrono::duration<double, std::milli>(clock::now() - start).count(); | |
| start = clock::now(); | |
| auto it = do_find(cont); | |
| assert(it != cont.end()); | |
| auto find_dur = std::chrono::duration<double, std::milli>(clock::now() - start).count(); | |
| std::cout << std::setprecision(5) | |
| << "iter ms= " << std::setw(7) << iter_dur | |
| << "\tfind ms= " << std::setw(7) << find_dur | |
| << "\telems= " << cont.size() << " sum= " << sum << " find=" << *it | |
| << "\tcontainer= " << typeid(cont).name() | |
| << std::endl; | |
| } | |
| int main() | |
| { | |
| // for (size_t i = 0; i < 10; ++i) { | |
| benchmark<std::array<EL, SIZE>>(); | |
| benchmark<boost::container::vector<EL>>(); | |
| benchmark<std::unordered_set<EL>>(); | |
| benchmark<boost::container::flat_set<EL>>(); | |
| benchmark<std::deque<EL>>(); | |
| benchmark<std::set<EL>>(); | |
| benchmark<std::vector<EL>>(); | |
| // } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment