diff --git a/src/tests/util/list.cpp b/src/tests/util/list.cpp index c5de1a33e..520c6cdf6 100644 --- a/src/tests/util/list.cpp +++ b/src/tests/util/list.cpp @@ -8,6 +8,7 @@ Author: Leonardo de Moura #include "util/test.h" #include "util/list.h" #include "util/list_fn.h" +#include "util/timeit.h" using namespace lean; static void tst1() { @@ -114,6 +115,22 @@ static void tst9(int sz) { std::cout << l3 << "\n"; } +static void tst10(int sz, int num) { + list l; + for (int i = 0; i < sz; i++) { + l = cons(i, l); + } + { + timeit timer(std::cout, "for_each"); + unsigned sum = 0; + for (int i = 0; i < num; i++) { + sum = 0; + for_each(l, [&](int v) { sum += v; }); + } + std::cout << "sum: " << sum << "\n"; + } +} + int main() { tst1(); tst2(); @@ -124,5 +141,6 @@ int main() { tst7(); tst8(); tst9(100); + tst10(1000, 5); return has_violations() ? 1 : 0; } diff --git a/src/util/list_fn.h b/src/util/list_fn.h index 0be40ee56..3b105e681 100644 --- a/src/util/list_fn.h +++ b/src/util/list_fn.h @@ -160,11 +160,11 @@ template void for_each(list const & l, F f) { static_assert(std::is_same::type, void>::value, "for_each: return type of f is not void"); - if (is_nil(l)) { - return; - } else { - f(head(l)); - return for_each(tail(l), f); + typedef typename list::cell cell; + cell * it = l.raw(); + while (it) { + f(it->head()); + it = it->tail().raw(); } }