Skip to content

Instantly share code, notes, and snippets.

@aldanor
Created August 17, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldanor/c8d2ea07132db536bb1645bc97aefb6c to your computer and use it in GitHub Desktop.
Save aldanor/c8d2ea07132db536bb1645bc97aefb6c to your computer and use it in GitHub Desktop.
ranges::any_view bench
#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;
)
}
@aldanor
Copy link
Author

aldanor commented Aug 17, 2016

Output:

856ms
115ms
113ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment