From d05deeaea79850f1df636540f1a916b3fed150c0 Mon Sep 17 00:00:00 2001 From: Alfredo Beaumont Date: Tue, 27 Aug 2019 19:35:04 +0200 Subject: [PATCH] Use threading to further decrease benchmark running time. --- binary-trees.dylan | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/binary-trees.dylan b/binary-trees.dylan index 46534b6..1441e76 100644 --- a/binary-trees.dylan +++ b/binary-trees.dylan @@ -7,7 +7,9 @@ end library; define module binary-trees use common-dylan, exclude: { format-to-string }; + use format; use format-out; + use threads; end module; define constant = false-or(); @@ -43,14 +45,27 @@ begin let long-lived-tree = build(max-depth); - for (d from min-depth to max-depth by 2) + let size = floor/(max-depth - min-depth, 2) + 1; + let messages = make(, size: size); + let threads = make(, size: size); + for (i from 0, + d from min-depth to max-depth by 2) let iterations = ash(1, max-depth - d + min-depth); - for (i from 1 to iterations, - c = 0 then c + build(d).check) - finally - format-out("%d\t trees of depth %d\t check: %d\n", iterations, d, c); - end for; + local method loop() + for (_ from 1 to iterations, + c = 0 then c + build(d).check) + finally + messages[i] := format-to-string("%d\t trees of depth %d\t check: %d", iterations, d, c); + end for; + end; + threads[i] := make(, function: loop); end for; + for (thread in threads) + thread & join-thread(thread); + end; + for (message in messages) + format-out("%s\n", message); + end; format-out("long lived tree of depth %d\t check: %d\n", max-depth, long-lived-tree.check);