Skip to content

Instantly share code, notes, and snippets.

@stdk
Last active December 29, 2015 11:49
Show Gist options
  • Save stdk/7665958 to your computer and use it in GitHub Desktop.
Save stdk/7665958 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <iostream>
#include <vector>
#include <numeric>
#include <chrono>
using std::vector;
class gen {
size_t value;
public:
inline gen(size_t v):value(v) {
}
inline bool operator==(const gen &other) {
return value == other.value;
}
inline bool operator!=(const gen &other) {
return value != other.value;
}
inline gen operator++() {
return gen(++value);
}
inline gen operator++(int) {
return gen(value++);
}
inline size_t operator*() {
return value*2;
}
};
template<class Operation>
auto measure(Operation op) -> decltype(std::chrono::steady_clock::duration::zero()) {
auto total_elapsed = std::chrono::steady_clock::duration::zero();
for(size_t i=0; i<1000000;i++) {
auto begin = std::chrono::steady_clock::now();
op();
total_elapsed += std::chrono::steady_clock::now() - begin;
}
return total_elapsed;
}
int main() {
//vector<bool> vector(100000000, true); 1
auto total = measure([]() {
std::accumulate(gen(0),gen(8589934591),0);
});
std::cout << total.count() << std::endl;
total = measure([]() {
size_t sum = 0;
for(size_t i=0; i<=8589934591;i++) {
sum += 2*i;
}
});
std::cout << total.count() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment