From 515eaae6172e55bac85993008749873c49a1ec29 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Mon, 7 Sep 2020 01:49:06 +0200 Subject: [PATCH] :white_check_mark: binary trees chuck --- benchmark/binary-trees.ck | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 benchmark/binary-trees.ck diff --git a/benchmark/binary-trees.ck b/benchmark/binary-trees.ck new file mode 100644 index 00000000..bbfa5dbc --- /dev/null +++ b/benchmark/binary-trees.ck @@ -0,0 +1,48 @@ +class Tree { + int item; + Tree @ left, right; + + fun static Tree new_Tree(int it, int depth) { + Tree t; + it => t.item; + if (depth > 0) { + it + it => int item2; + --depth; + new_Tree(item2 - 1, depth) @=> t.left; + new_Tree(item2, depth) @=> t.right; + } + return t; + } + + fun int check() { + if (left == NULL) + return item; + return item + left.check() - right.check(); + } +} + +4 => int minDepth; +12 => int maxDepth; +maxDepth + 1 => int stretchDepth; + +<<< "stretch tree of depth ", stretchDepth, " check: ", + Tree.new_Tree(0, stretchDepth).check() >>>; + +Tree.new_Tree(0, maxDepth) @=> Tree @ longLivedTree; + +1 => int iterations; +for (int d; d < maxDepth; ++d) + 2 *=> iterations; + +minDepth => int depth; +while (depth < stretchDepth) { + int check; + for (int i; i < iterations; ++i) + Tree.new_Tree(i, depth).check() + Tree.new_Tree(-i, depth).check() +=> check; + + <<< iterations * 2, " trees of depth ", depth, " check: ", check >>>; + 4 /=> iterations; + 2 +=> depth; +} + +<<< "long lived tree of depth ", maxDepth, " check: ", longLivedTree.check() >>>; -- 2.43.0