changeset 11:efa39c007546

Improve timeit_f interface
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 18 Mar 2023 22:51:52 +0100
parents 3ad8456efad1
children c8e1da3603ea
files dynprog.cc lib/exec.h
diffstat 2 files changed, 12 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/dynprog.cc	Sat Mar 18 22:25:57 2023 +0100
+++ b/dynprog.cc	Sat Mar 18 22:51:52 2023 +0100
@@ -146,10 +146,10 @@
     vector<T> a(v.begin(), v.end());
     size_t longest = 0;
     vector<size_t> seqixs(a.size());
-    size_t result = timeit_f(function([&a, &longest, &seqixs]() -> size_t {
+    size_t result = timeit_f([&a, &longest, &seqixs]() -> size_t {
                 return longest_increasing_subsequence(
                         a, 0, 0, 0, longest, seqixs);
-                }),
+                },
             "longest incr. subsequence");
 
     fmt::print("Longest incr. subsequence has length {}/{} and has indices: {}\n",
@@ -169,10 +169,10 @@
     T highest = 0;
     vector<size_t> seqixs(a.size());
     size_t result =
-        timeit_f(function([&a, &highest, &seqixs]() -> size_t {
+        timeit_f([&a, &highest, &seqixs]() -> size_t {
                     return max_sum_incr_subseq(a, 0, 0, 0, static_cast<T>(0),
                             highest, seqixs);
-                    }),
+                    },
                 "max sum incr. subsequence");
 
     fmt::print(
@@ -221,9 +221,9 @@
     if (n == 0)
         return 1;
 
-    /*const auto memopos = memo.find(make_pair(last, n));
+    const auto memopos = memo.find(make_pair(last, n));
       if (memopos != memo.end())
-      return memopos->second;*/
+      return memopos->second;
 
     if (last < 0) {
         const auto ks = views::keys(adj);
@@ -260,9 +260,10 @@
     //keypad(adjacency, vector<int>{}, n);
     cout << endl;
 
-    size_t ck = timeit_f(function([&adjacency, &memo, n]() { return count_keypad(adjacency, -1, n, memo); }), "count_keypad");
+    size_t ck = timeit_f(([&adjacency, &memo, n]() { return count_keypad(adjacency, -1, n, memo); }), "count_keypad");
     fmt::print("count = {}\n", ck);
 
+    fmt::print("memoize count = {}\n", memo.size());
 }
 
 int lcss_main(void) {
@@ -287,6 +288,6 @@
     vector<int> a{-1, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11};
     // Expect: 0,2,6,9,13 or ,11
     // or 0,4,6,9,13
-    evaluate_keypad(6);
+    evaluate_keypad(10);
     return 0;
 }
--- a/lib/exec.h	Sat Mar 18 22:25:57 2023 +0100
+++ b/lib/exec.h	Sat Mar 18 22:51:52 2023 +0100
@@ -12,11 +12,11 @@
 
 using std::function, std::string, std::chrono::high_resolution_clock;
 
-template<typename T>
-T timeit_f(function<T()> f, string name = string()) {
+template<typename F>
+auto timeit_f(F f, string name = string()) -> decltype(f()) {
     auto begin = high_resolution_clock::now();
 
-    T result = f();
+    auto result = f();
 
     auto after = high_resolution_clock::now();