Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <chrono>
- void bench(size_t iterations, std::function<void ()> func)
- {
- using namespace std::chrono;
- auto start = high_resolution_clock::now();
- for(size_t i = 0; i < iterations; ++i)
- {
- func();
- }
- auto stop = high_resolution_clock::now();
- nanoseconds duration = stop - start;
- std::cout << "Ran " << iterations << " iterations, which took " <<
- duration.count() << "ns" << std::endl;
- if(iterations>1)
- {
- std::cout << "Which is " << duration.count()/iterations << "ns per iteration " << std::endl;
- }
- }
- class range {
- int b, e;
- public:
- class iterator
- {
- int value;
- public:
- iterator(int v) : value(v) {}
- bool operator==(const iterator& other) const { return value == other.value; }
- bool operator!=(const iterator& other) const { return value != other.value; }
- const iterator& operator++() { value++; return *this; }
- const iterator& operator--() { value--; return *this; }
- int operator*() const { return value; }
- };
- range(int b_, int e_) : b(b_), e(e_) {}
- iterator begin() { return b; }
- iterator end() { return e+1; }
- };
- int main()
- {
- int ITER = 10000;
- bench(ITER, [](){
- int sum = 0;
- for( auto i : range(1,1000) )
- {
- sum += i;
- }
- });
- bench(ITER, [](){
- int sum = 0;
- for(int i = 1; i < 1000; ++i)
- {
- sum += i;
- }
- });
- return 0;
- }
Add Comment
Please, Sign In to add comment