changeset 1:ec6009463e1c

Timing infrastructure, cmake build
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 05 Mar 2023 10:01:51 +0100
parents 85a51415f042
children ed80bcd28852
files .clangd .hgignore CMakeLists.txt arrays.cc lib/CMakeLists.txt lib/exec.cc lib/exec.h time.cc
diffstat 8 files changed, 153 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.clangd	Sun Mar 05 10:01:51 2023 +0100
@@ -0,0 +1,2 @@
+CompileFlags:
+    Add: [-std=c++20]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Sun Mar 05 10:01:51 2023 +0100
@@ -0,0 +1,1 @@
+build
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeLists.txt	Sun Mar 05 10:01:51 2023 +0100
@@ -0,0 +1,11 @@
+cmake_minimum_required(VERSION 3.26)
+project(ccplay)
+
+ADD_SUBDIRECTORY(lib)
+
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -O")
+ADD_EXECUTABLE(time time.cc)
+TARGET_LINK_LIBRARIES(time exec)
+
+ADD_EXECUTABLE(arrays arrays.cc)
+TARGET_LINK_LIBRARIES(arrays exec)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arrays.cc	Sun Mar 05 10:01:51 2023 +0100
@@ -0,0 +1,64 @@
+#include <iostream>
+#include <vector>
+#include <unordered_set>
+#include <tuple>
+#include "lib/exec.h"
+
+using namespace std;
+
+template<typename T>
+vector<pair<T,T>> twosum(const vector<T>& v, T sum) {
+    unordered_set<T> rem;
+    vector<pair<T,T>> pairs;
+    for (const T& e : v) {
+        if (rem.count(sum-e) > 0) {
+            pairs.push_back(make_pair(e, sum-e));
+        } else {
+            rem.insert(e);
+        }
+    }
+    return pairs;
+}
+
+template<typename T>
+void threesum(const vector<T>& v, T sum) {
+    vector<tuple<T,T,T>> triples;
+    for (const T& e : v) {
+        T rem = sum - e;
+        auto pairs = twosum(v, rem);
+        for (const pair<T,T>& p : pairs) {
+            triples.push_back(make_tuple(p.first, p.second, e));
+        }
+    }
+    /*
+    for (const auto& t : triples) {
+        cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << endl;
+    }
+    */
+}
+
+int twosum_entry(void) {
+    vector<int> v{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,-1,-2,-3,-4,-5,-6,-7,-8,-9};
+    benchmarkit([&v]() { twosum(v, 20); });
+    return 0;
+}
+int threesum_entry(void) {
+    vector<int> v{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,-1,-2,-3,-4,-5,-6,-7,-8,-9};
+    benchmarkit([&v]() { threesum(v, 20); });
+    return 0;
+}
+
+int main(int argc, char** argv) {
+    if (argc < 1) {
+        cerr << "need task supplied on command line!\n";
+        return 1;
+    }
+    string task(argv[1]);
+
+    if (task == "twosum")
+        twosum_entry();
+    else if (task == "threesum")
+        threesum_entry();
+
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/CMakeLists.txt	Sun Mar 05 10:01:51 2023 +0100
@@ -0,0 +1,5 @@
+
+cmake_minimum_required(VERSION 3.26)
+
+ADD_LIBRARY(exec exec.cc)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/exec.cc	Sun Mar 05 10:01:51 2023 +0100
@@ -0,0 +1,50 @@
+
+#include "exec.h"
+
+#include <chrono>
+#include <iostream>
+
+using namespace std;
+using namespace std::chrono;
+
+void timeit(function<void()> f, string name) {
+    auto begin = high_resolution_clock::now();
+
+    f();
+
+    auto after = high_resolution_clock::now();
+
+    auto d = after - begin;
+    decltype(d)::rep count = d.count();
+    decltype(d)::period p;
+
+    cout << name << " :: " << static_cast<double>(p.num)/p.den * count << endl;
+}
+
+void benchmarkit(function<void()> f, string name, function<void()> setup) {
+
+    int n = 1;
+    const double min_duration = 0.1;
+
+    while (true) {
+        setup();
+     
+        cout << "Running " << name << " for " << n << " iterations.\n";
+
+        auto begin = high_resolution_clock::now();
+
+        for (unsigned int i = 0; i < n; i += 1)
+            f();
+
+        auto end = high_resolution_clock::now();
+        decltype(end-begin)::period p;
+        auto seconds = (end-begin).count() * static_cast<double>(p.num)/p.den;
+        if (seconds < min_duration) {
+            n *= 2;
+        } else {
+            cout << n << " iterations took " << seconds << " seconds (" << seconds/n << " s/iter)\n";
+            break;
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/exec.h	Sun Mar 05 10:01:51 2023 +0100
@@ -0,0 +1,7 @@
+
+#include <functional>
+#include <string>
+
+void timeit(std::function<void()> f, std::string name = std::string());
+
+void benchmarkit(std::function<void()> f, std::string name = std::string(), std::function<void()> setup = [](){});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/time.cc	Sun Mar 05 10:01:51 2023 +0100
@@ -0,0 +1,13 @@
+#include <chrono>
+#include <iostream>
+
+#include "lib/exec.h"
+
+using namespace std;
+
+int main(void) {
+
+    benchmarkit([]() { int a = 5 + 6; }, "simple add");
+
+    return 0;
+}