From 1206ea51138ba7bf9691dec21b1a5ca86998c9b6 Mon Sep 17 00:00:00 2001 From: fabiosvm Date: Sun, 29 Dec 2024 20:09:52 -0300 Subject: [PATCH 1/3] Add Hook benchmark --- Brewfile | 4 +++- fibonacci/hook/code.hk | 10 ++++++++++ hello-world/hook/code.hk | 1 + loops/hook/code.hk | 14 ++++++++++++++ run.sh | 1 + 5 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 fibonacci/hook/code.hk create mode 100644 hello-world/hook/code.hk create mode 100644 loops/hook/code.hk diff --git a/Brewfile b/Brewfile index 2a6ff9a5..c99a624d 100644 --- a/Brewfile +++ b/Brewfile @@ -17,4 +17,6 @@ cask "dotnet-sdk" brew "julia" brew "haskell-stack" brew "kotlin" -cask "kotlin-native" \ No newline at end of file +cask "kotlin-native" +tap "hook-lang/hook" +brew "hook" diff --git a/fibonacci/hook/code.hk b/fibonacci/hook/code.hk new file mode 100644 index 00000000..5c03f768 --- /dev/null +++ b/fibonacci/hook/code.hk @@ -0,0 +1,10 @@ +fn fibonacci(n) => + if (n < 2) n + else fibonacci(n - 1) + fibonacci(n - 2); + +let u = to_int(args[0]); +var r = 0; +for (var i = 1; i < u; i++) { + r += fibonacci(i); +} +println(r); diff --git a/hello-world/hook/code.hk b/hello-world/hook/code.hk new file mode 100644 index 00000000..f34c7f9e --- /dev/null +++ b/hello-world/hook/code.hk @@ -0,0 +1 @@ +println("Hello, World!"); diff --git a/loops/hook/code.hk b/loops/hook/code.hk new file mode 100644 index 00000000..98696fca --- /dev/null +++ b/loops/hook/code.hk @@ -0,0 +1,14 @@ +import { floor } from math; +import { rand } from numbers; +import { fill } from arrays; + +let u = to_int(args[0]); +var r = floor(rand() * 10000); +var a = fill(0, 10000); +for (var i = 0; i < 10000; i++) { + for (var j = 0; j < 100000; j++) { + a[i] = a[i] + j % u; + } + a[i] += r; +} +println(a[r]); diff --git a/run.sh b/run.sh index f6c14ee6..ab11f073 100755 --- a/run.sh +++ b/run.sh @@ -66,6 +66,7 @@ run "Free Pascal" "./fpc/code" "./fpc/code" "${input}" run "Go" "./go/code" "./go/code" "${input}" run "Haskell" "./haskell/code" "./haskell/code" "${input}" #run "Haxe JVM" "haxe/code.jar" "java -jar haxe/code.jar" "${input}" # was getting errors running `haxelib install hxjava` +run "Hook" "./hook/code.hk" "hook ./hook/code.hk" "${input}" run "Inko" "./inko/code" "./inko/code" "${input}" run "Java" "./jvm/code.class" "java jvm.code" "${input}" #run "Java Native" "./jvm.code" "./jvm.code" "${input}" From 3ca23b2036e7a65e828fdd7dfa91fd674aeb1678 Mon Sep 17 00:00:00 2001 From: fabiosvm Date: Tue, 31 Dec 2024 19:55:55 -0300 Subject: [PATCH 2/3] Update Hook implementations --- fibonacci/hook/code.hk | 8 +++++--- loops/hook/code.hk | 16 ++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/fibonacci/hook/code.hk b/fibonacci/hook/code.hk index 5c03f768..81656f48 100644 --- a/fibonacci/hook/code.hk +++ b/fibonacci/hook/code.hk @@ -1,6 +1,8 @@ -fn fibonacci(n) => - if (n < 2) n - else fibonacci(n - 1) + fibonacci(n - 2); +fn fibonacci(n) { + if (n == 0) return 0; + if (n == 1) return 1; + return fibonacci(n-1) + fibonacci(n-2); +} let u = to_int(args[0]); var r = 0; diff --git a/loops/hook/code.hk b/loops/hook/code.hk index 98696fca..47cc5d81 100644 --- a/loops/hook/code.hk +++ b/loops/hook/code.hk @@ -2,13 +2,13 @@ import { floor } from math; import { rand } from numbers; import { fill } from arrays; -let u = to_int(args[0]); -var r = floor(rand() * 10000); -var a = fill(0, 10000); -for (var i = 0; i < 10000; i++) { - for (var j = 0; j < 100000; j++) { - a[i] = a[i] + j % u; +let u = to_int(args[0]); // Get an input number from the command line +var r = floor(rand() * 10000); // Get a random number 0 <= r < 10k +var a = fill(0, 10000); // Array of 10k elements initialized to 0 +for (var i = 0; i < 10000; i++) { // 10k outer loop iterations + for (var j = 0; j < 100000; j++) { // 100k inner loop iterations, per outer loop iteration + a[i] = a[i] + j%u; // Simple sum } - a[i] += r; + a[i] += r; // Add a random value to each element in array } -println(a[r]); +println(a[r]); // Print out a single element from the array From 4ece6c3a10d7d2ab935230ff770af4f14e0bb44a Mon Sep 17 00:00:00 2001 From: fabiosvm Date: Wed, 1 Jan 2025 15:45:24 -0300 Subject: [PATCH 3/3] Add levenshtein implementation in Hook --- levenshtein/hook/code.hk | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 levenshtein/hook/code.hk diff --git a/levenshtein/hook/code.hk b/levenshtein/hook/code.hk new file mode 100644 index 00000000..bb70d370 --- /dev/null +++ b/levenshtein/hook/code.hk @@ -0,0 +1,73 @@ +import { new_array, fill } from arrays; + +// Can either define your own min function +// or use a language / standard library function +fn min(a, b, c) { + var min = a; + if (b < min) min = b; + if (c < min) min = c; + return min; +} + +fn levenshtein_distance(str1t, str2t) { + // Get lengths of both strings + let mt = len(str1t); + let nt = len(str2t); + // Assign shorter one to str1, longer one to str2 + let str1 = if (mt <= nt) str1t else str2t; + let str2 = if (mt <= nt) str2t else str1t; + // store the lengths of shorter in m, longer in n + let m = if (str1 == str1t) mt else nt; + let n = if (str1 == str1t) nt else mt; + + // Create two rows, previous and current + var prev = new_array(m+1); + var curr = fill(0, m+1); + + // initialize the previous row + for (var i = 0; i <= m; i++) { + prev[] = i; + } + + // Iterate and compute distance + for (var i = 1; i <= n; i++) { + curr[0] = i; + for (var j = 1; j <= m; j++) { + let cost = if (str1[j-1] == str2[i-1]) 0 else 1; + curr[j] = min( + prev[j] + 1, // Deletion + curr[j-1] + 1, // Insertion + prev[j-1] + cost // Substitution + ); + } + for (var j = 0; j <= m; j++) { + prev[j] = curr[j]; + } + } + + // Return final distance, stored in prev[m] + return prev[m]; +} + +var min_distance = -1; +var times = 0; +// Iterate through all combinations of command line args +let argc = len(args); +for (var i = 0; i < argc; i++) { + for (var j = 0; j < argc; j++) { + // Don't compare the same string to itself + if (i != j) { + let distance = levenshtein_distance(args[i], args[j]); + if (min_distance == -1 || min_distance > distance) { + min_distance = distance; + } + times++; + } + } +} + +// The only output from the program should be the times (number of comparisons) +// and min distance calculated of all comparisons. Two total lines of output, +// formatted exactly like this. +println("times: " + to_string(times)); +println("min_distance: " + to_string(min_distance));