Skip to content

Commit

Permalink
Merge pull request #6 from TheLartians/dev
Browse files Browse the repository at this point in the history
update benchmark and comments
  • Loading branch information
TheLartians authored May 9, 2019
2 parents c772c18 + 72330e9 commit 4d59ba6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
29 changes: 18 additions & 11 deletions benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Integer __attribute__((noinline)) easyRangeLoop(Integer max){
}

void EasyRangeLoop(benchmark::State& state) {
Integer max = 100000;
benchmark::DoNotOptimize(max);
Integer max = 10000;
for (auto _ : state) {
benchmark::DoNotOptimize(max);
AssertEqual(easyRangeLoop(max),max*(max+1)/2);
}
}
Expand All @@ -33,15 +33,18 @@ BENCHMARK(EasyRangeLoop);

Integer __attribute__((noinline)) easyCustomRangeLoop(Integer max){

struct CustomRangeIterator {
struct CustomRangeIterator: public easy_iterator::InitializedIterable {
Integer current, max, step;

CustomRangeIterator(Integer start,Integer end,Integer increment):
current(start),
max(end - ((end - start) % increment)),
step(increment){
}

CustomRangeIterator(Integer start,Integer end):CustomRangeIterator(start,end,1){ }
CustomRangeIterator(Integer max):CustomRangeIterator(0,max,1){ }

bool init(){ return current != max; }
bool advance(){ current += step; return current != max; }
Integer value(){ return current; }
Expand All @@ -56,9 +59,9 @@ Integer __attribute__((noinline)) easyCustomRangeLoop(Integer max){
}

void EasyCustomRangeLoop(benchmark::State& state) {
Integer max = 100000;
benchmark::DoNotOptimize(max);
Integer max = 10000;
for (auto _ : state) {
benchmark::DoNotOptimize(max);
AssertEqual(easyCustomRangeLoop(max),max*(max+1)/2);
}
}
Expand All @@ -74,9 +77,9 @@ Integer __attribute__((noinline)) forLoop(Integer max){
}

void ForLoop(benchmark::State& state) {
Integer max = 100000;
benchmark::DoNotOptimize(max);
Integer max = 10000;
for (auto _ : state) {
benchmark::DoNotOptimize(max);
AssertEqual(forLoop(max),max*(max+1)/2);
}
}
Expand All @@ -96,8 +99,8 @@ void EasyArrayIteration(benchmark::State& state) {
Integer max = 10000;
std::vector<int> values(max+1);
easy_iterator::copy(easy_iterator::range(max+1), values);
benchmark::DoNotOptimize(values);
for (auto _ : state) {
benchmark::DoNotOptimize(values);
AssertEqual(easyArrayIteration(values) ,max*(max+1)/2);
}
}
Expand All @@ -117,8 +120,8 @@ void StdArrayIteration(benchmark::State& state) {
Integer max = 10000;
std::vector<int> values(max+1);
easy_iterator::copy(easy_iterator::range(max+1), values);
benchmark::DoNotOptimize(values);
for (auto _ : state) {
benchmark::DoNotOptimize(values);
AssertEqual(stdArrayIteration(values),max*(max+1)/2);
}
}
Expand All @@ -138,6 +141,8 @@ void EasyZipIteration(benchmark::State& state) {
easy_iterator::copy(easy_iterator::range(size), integers);
easy_iterator::copy(easy_iterator::range(size), doubles);
for (auto _ : state) {
benchmark::DoNotOptimize(integers);
benchmark::DoNotOptimize(doubles);
easyZipIteration(integers, doubles);
}
}
Expand All @@ -161,6 +166,8 @@ void StdZipIteration(benchmark::State& state) {
easy_iterator::copy(easy_iterator::range(size), integers);
easy_iterator::copy(easy_iterator::range(size), doubles);
for (auto _ : state) {
benchmark::DoNotOptimize(integers);
benchmark::DoNotOptimize(doubles);
stdZipIteration(integers, doubles);
}
}
Expand All @@ -177,8 +184,8 @@ void EasyEnumerateIteration(benchmark::State& state) {
Integer max = 10000;
std::vector<int> values(max);
easy_iterator::copy(easy_iterator::range(max), values);
benchmark::DoNotOptimize(values);
for (auto _ : state) {
benchmark::DoNotOptimize(values);
easyEnumerateIteration(values);
}
}
Expand All @@ -197,8 +204,8 @@ void ManualEnumerateIteration(benchmark::State& state) {
Integer max = 10000;
std::vector<int> values(max);
easy_iterator::copy(easy_iterator::range(max), values);
benchmark::DoNotOptimize(values);
for (auto _ : state) {
benchmark::DoNotOptimize(values);
manualEnumerateIteration(values);
}
}
Expand Down
5 changes: 4 additions & 1 deletion include/easy_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ namespace easy_iterator {
*/
template <class T> struct RangeIterator final: public IteratorPrototype<T, dereference::ByValue> {
T increment;

RangeIterator(const T &start, const T &_increment = 1):
IteratorPrototype<T, dereference::ByValue>(start),
increment(_increment) {
Expand Down Expand Up @@ -434,7 +435,8 @@ namespace easy_iterator {
}

/**
* Returns a pointer to the value if found
* Returns a pointer to the value if found, otherwise `nullptr`.
* Usage: `if(auto v = found(map.find(key),map)){ do_something(v); }`
*/
template <class I, class C> decltype(&*std::declval<I>()) found(const I &it, C &container){
if (it != container.end()) {
Expand All @@ -446,6 +448,7 @@ namespace easy_iterator {

/**
* Removes a value from a container with `find` method.
* Usage: `eraseIfFound(map.find(key),map);`
*/
template <class I, class C> bool eraseIfFound(const I &it, C &container){
if (it != container.end()) {
Expand Down

0 comments on commit 4d59ba6

Please sign in to comment.