Created
August 17, 2016 22:20
-
-
Save aldanor/c8d2ea07132db536bb1645bc97aefb6c to your computer and use it in GitHub Desktop.
ranges::any_view bench
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 <iostream> | |
#include <chrono> | |
#include "range-v3/include/range/v3/all.hpp" | |
using namespace std::chrono; | |
template<typename T, CONCEPT_REQUIRES_(ranges::Integral<T>())> | |
class odd : public ranges::view_facade<odd<T>> { | |
friend ranges::range_access; | |
T max_, curr_; | |
const T& get() const { return curr_; } | |
bool done() const { return curr_ >= max_; } | |
void next() { curr_ += 2; } | |
public: | |
odd() = default; | |
odd(T min, T max) : max_(max), curr_(min % 2 ? min : min + 1) { } | |
}; | |
#define BENCH(what) { \ | |
auto t0 = high_resolution_clock::now(); \ | |
what \ | |
auto t1 = high_resolution_clock::now(); \ | |
std::cout << duration_cast<milliseconds>(t1 - t0).count() << "ms" << std::endl; \ | |
} | |
int main() { | |
using T = unsigned int; | |
T N = 100000000; | |
BENCH( | |
ranges::any_view<const T&> r = odd<T>(0, N); | |
volatile T s = 0; | |
RANGES_FOR(auto x, r) | |
s += x; | |
) | |
BENCH( | |
auto r = odd<T>(0, N); | |
volatile T s = 0; | |
RANGES_FOR(auto x, r) | |
s += x; | |
) | |
BENCH( | |
volatile T s = 0; | |
for (T x = 1; x < N; x += 2) | |
s += x; | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: