From de200aac4cd7b69a48494daae3628841f5420937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Wed, 4 Oct 2023 22:10:01 +0200 Subject: [PATCH] Add a new 'btree' benchmark, that does a lot of object creation and recursive function calls. [BENCHMARKS] * NEW: Add a new 'btree' benchmark, that does a lot of object creation and recursive function calls. * NEW: Rename the 'string' benchmark as 'string1', to avoid name clashes in Python. * BUG: Fix the HTML table generation. --- benchmark/benchmark.gbs | 81 ++++++++++++++------------- benchmark/btree.gbs | 69 +++++++++++++++++++++++ benchmark/btree.jvs | 52 +++++++++++++++++ benchmark/btree.pl | 47 ++++++++++++++++ benchmark/btree.py | 44 +++++++++++++++ benchmark/mandelbrot.jvs | 1 + benchmark/{string.gbs => string1.gbs} | 0 benchmark/{string.jvs => string1.jvs} | 0 benchmark/{string.pl => string1.pl} | 0 benchmark/{string.py => string1.py} | 3 +- 10 files changed, 257 insertions(+), 40 deletions(-) create mode 100755 benchmark/btree.gbs create mode 100755 benchmark/btree.jvs create mode 100755 benchmark/btree.pl create mode 100755 benchmark/btree.py rename benchmark/{string.gbs => string1.gbs} (100%) rename benchmark/{string.jvs => string1.jvs} (100%) rename benchmark/{string.pl => string1.pl} (100%) rename benchmark/{string.py => string1.py} (88%) diff --git a/benchmark/benchmark.gbs b/benchmark/benchmark.gbs index 1076613b7..e0d8aded7 100755 --- a/benchmark/benchmark.gbs +++ b/benchmark/benchmark.gbs @@ -1,10 +1,10 @@ #!/usr/bin/env gbs3 -Private $fCompileTime as Float +Private $fCompileTime As Float Private Sub RunScript(sScript As String, sRun As String, bGambasOnly As Boolean) As Float - Dim aResult as String[] + Dim aResult As String[] Dim sResult As String Dim fTime As Float @@ -39,10 +39,10 @@ Private Sub FormatResult(cResult As Collection, iCount As Integer, sLang As Stri Dim sFormat As String Dim sTest As String - If cResult[sLang] <= 0 Then Return "-" + If cResult[sLang] <= 0 Then Return Format("30", "0.00") & "+" - For Each sTest In ["python", "perl", "java","gambas"] - If cResult[sTest] < cResult[sLang] Then + For Each sTest In ["python", "perl", "java", "gambas"] + If cResult[sTest] > 0 And If cResult[sTest] < cResult[sLang] Then bBetter = False Break Endif @@ -66,15 +66,17 @@ Dim iCount As Integer = 1 Dim I As Integer Dim fTime As Float +Shell "rm -rf " & Shell$(Application.Dir &/ " __pycache__") Wait + For I = 1 To Args.Max If Args[I] = "-gambas" Then bGambasOnly = True - Else If Args[I] = "-count" Then - If I < Args.Max And If Args[I + 1] Not Begins "-" Then - Try iCount = Max(1, CInt(Args[I + 1])) - inc I - Endif + Else If Args[I] = "-count" Then + If I < Args.Max And If Args[I + 1] Not Begins "-" Then + Try iCount = Max(1, CInt(Args[I + 1])) + Inc I + Endif Else sBenchmarkOnly = Args[I] Endif @@ -83,37 +85,40 @@ Next For I = 1 To iCount - If iCount > 1 Then Print "-------- Run #"; I;" --------" + If iCount > 1 Then Print "-------- Run #"; I; " --------" - For Each sBenchmark In Dir(Application.Dir, "*.gbs").Sort() + For Each sBenchmark In Dir(Application.Dir, "*.gbs").Sort() - sBenchmark = File.BaseName(sBenchmark) - If sBenchmark = "benchmark" Then Continue - If sBenchmarkOnly And If sBenchmark <> sBenchmarkOnly Then Continue + sBenchmark = File.BaseName(sBenchmark) + If sBenchmark = "benchmark" Then Continue + If sBenchmarkOnly And If sBenchmark <> sBenchmarkOnly Then Continue - cResult = cAllResult[sBenchmark] - If Not cResult Then - cResult = New Collection - cResult.Default = 0.0 - cResult!name = sBenchmark - cAllResult[sBenchmark] = cResult - Endif - - If Not bGambasOnly Then - cResult!python += RunScript(sBenchmark & ".py", "python", bGambasOnly) - cResult!perl += RunScript(sBenchmark & ".pl", "perl", bGambasOnly) - If sBenchmark Not Begins "string" Then - cResult!java += RunScript(sBenchmark & ".jvs", "java -Xint --source 11", bGambasOnly) - Endif - cResult!javajit += RunScript(sBenchmark & ".jvs", "java --source 11", bGambasOnly) - Endif - fTime = RunScript(sBenchmark & ".gbs", "gbs3 -f -U -c", bGambasOnly) - cResult!gambasjit += fTime - cResult!gambasjitwct += fTime - $fCompileTime - cResult!gambas += RunScript(sBenchmark & ".gbs", "gbs3 -c", bGambasOnly) + cResult = cAllResult[sBenchmark] + If Not cResult Then + cResult = New Collection + cResult.Default = 0.0 + cResult!name = sBenchmark + cAllResult[sBenchmark] = cResult + Endif + + If Not bGambasOnly Then + If sBenchmark <> "string2" Then + cResult!python += RunScript(sBenchmark & ".py", "python", bGambasOnly) + Endif + cResult!perl += RunScript(sBenchmark & ".pl", "perl", bGambasOnly) + If sBenchmark Not Begins "string" Then + cResult!java += RunScript(sBenchmark & ".jvs", "java -Xint --source 11", bGambasOnly) + Endif + cResult!javajit += RunScript(sBenchmark & ".jvs", "java --source 11", bGambasOnly) + Endif - Next - + fTime = RunScript(sBenchmark & ".gbs", "gbs3 -f -U -c", bGambasOnly) + cResult!gambasjit += fTime + cResult!gambasjitwct += fTime - $fCompileTime + cResult!gambas += RunScript(sBenchmark & ".gbs", "gbs3 -c", bGambasOnly) + + Next + Next Print @@ -139,7 +144,7 @@ Print If bGambasOnly Then For Each cResult In cAllResult - Print cResult!name; Space$(20 - Len(cResult!name)); ": "; Format(cResult!gambas / iCount, "0.00"); " / ";Format(cResult!gambasjit / iCount, "0.00");" (jit)"; " / ";Format(cResult!gambasjitwct / iCount, "0.00");" (jit without compilation time)" + Print cResult!name; Space$(20 - Len(cResult!name)); ": "; Format(cResult!gambas / iCount, "0.00"); " / "; Format(cResult!gambasjit / iCount, "0.00"); " (jit)"; " / "; Format(cResult!gambasjitwct / iCount, "0.00"); " (jit without compilation time)" Next Else diff --git a/benchmark/btree.gbs b/benchmark/btree.gbs new file mode 100755 index 000000000..2a340c6e1 --- /dev/null +++ b/benchmark/btree.gbs @@ -0,0 +1,69 @@ +#!/usr/bin/env gbs3 + +Class TreeNode + + Public Left As TreeNode + Public Right As TreeNode + + Static Public Sub Create(D As Integer) As TreeNode + + Return ChildTreeNodes(D) + + End + + Static Public Sub ChildTreeNodes(D As Integer) As TreeNode + + Dim hNode As TreeNode = new TreeNode + + If D > 0 Then + + hNode.Left = ChildTreeNodes(D - 1) + hNode.Right = ChildTreeNodes(D - 1) + + Endif + + Return hNode + + End + + Public Sub Check() As Integer + + If Not Left Then Return 1 + Return Left.Check() + Right.Check() + 1 + + End + +End Class + +Dim iMinDepth As Integer = 4 +Dim iMaxDepth As Integer = 16 +Dim iStretchDepth As Integer = iMaxDepth + 1 +Dim iCheck As Integer +Dim hTree As TreeNode +Dim D, I As Integer +Dim iIterations As Integer + +iCheck = TreeNode.Create(iStretchDepth).Check() + +Print "stretch tree of depth "; iMaxDepth + 1; "\t check: "; iCheck + +hTree = TreeNode.Create(iMaxDepth) + +For D = iMinDepth To iMaxDepth Step 2 + + iCheck = 0 + iIterations = Shl(1, iMaxDepth - D + iMinDepth) + + For I = 1 To iIterations + + iCheck += TreeNode.Create(D).Check() + + Next + + Print iIterations;"\t trees of depth "; D; "\t check: ";iCheck + +Next + +Print "long lived tree of depth "; iMaxDepth; "\t check: "; hTree.Check() + +Error CStr(Jit.Time) diff --git a/benchmark/btree.jvs b/benchmark/btree.jvs new file mode 100755 index 000000000..ae44da4dc --- /dev/null +++ b/benchmark/btree.jvs @@ -0,0 +1,52 @@ +#!/usr/bin/java --source 11 + +public class binarytrees { + public static void main(String[] args) throws Exception { + int n = args.length > 0 ? Integer.parseInt(args[0]) : 0; + int minDepth = 4; + int maxDepth = 16; + int stretchDepth = maxDepth + 1; + int check = (TreeNode.create(stretchDepth)).check(); + + System.out.println("stretch tree of depth " + (maxDepth + 1) + "\t check: " + check); + + TreeNode longLivedTree = TreeNode.create(maxDepth); + for (int depth = minDepth; depth <= maxDepth; depth += 2) + { + int iterations = 1 << (maxDepth - depth + minDepth); + check = 0; + + for (int i = 1; i <= iterations; i++) + { + check += (TreeNode.create(depth)).check(); + } + System.out.println(iterations + "\t trees of depth " + depth + "\t check: " + check); + } + + System.out.println("long lived tree of depth " + maxDepth + "\t check: " + longLivedTree.check()); + } + + static class TreeNode { + TreeNode left, right; + + static TreeNode create(int depth) + { + return ChildTreeNodes(depth); + } + + static TreeNode ChildTreeNodes(int depth) + { + TreeNode node = new TreeNode(); + if (depth > 0) + { + node.left = ChildTreeNodes(depth - 1); + node.right = ChildTreeNodes(depth - 1); + } + return node; + } + + int check() { + return left == null ? 1 : left.check() + right.check() + 1; + } + } +} diff --git a/benchmark/btree.pl b/benchmark/btree.pl new file mode 100755 index 000000000..e70b3a9a0 --- /dev/null +++ b/benchmark/btree.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl -w + +run( @ARGV ); + +sub bottomup_tree { + my $depth = shift; + return 0 unless $depth; + --$depth; + [ bottomup_tree($depth), bottomup_tree($depth) ]; +} + +sub check_tree { + return 1 unless ref $_[0]; + 1 + check_tree($_[0][0]) + check_tree($_[0][1]); +} + +sub stretch_tree { + my $stretch_depth = shift; + my $stretch_tree = bottomup_tree($stretch_depth); + print "stretch tree of depth $stretch_depth\t check: ", + check_tree($stretch_tree), "\n"; +} + +sub run { + my $max_depth = 16; + my $min_depth = 4; + + $max_depth = $min_depth + 2 if $min_depth + 2 > $max_depth; + + stretch_tree( $max_depth + 1 ); + + my $longlived_tree = bottomup_tree($max_depth); + + for ( my $depth = $min_depth; $depth <= $max_depth; $depth += 2 ) { + my $iterations = 2**($max_depth - $depth + $min_depth); + my $check = 0; + + foreach (1..$iterations) { + $check += check_tree( bottomup_tree($depth) ); + } + + print $iterations, "\t trees of depth $depth\t check: ", $check, "\n" + } + + print "long lived tree of depth $max_depth\t check: ", + check_tree($longlived_tree), "\n" +} diff --git a/benchmark/btree.py b/benchmark/btree.py new file mode 100755 index 000000000..78a49edad --- /dev/null +++ b/benchmark/btree.py @@ -0,0 +1,44 @@ +#!/usr/bin/python + +import sys + +class TreeNode: + left = None + right = None + + def check(self): + if self.left == None: + return 1 + return self.left.check() + self.right.check() + 1 + +def CreateTreeNode(depth): + return ChildTreeNodes(depth) + +def ChildTreeNodes(depth): + node = TreeNode() + if depth > 0: + node.left = ChildTreeNodes(depth - 1) + node.right = ChildTreeNodes(depth - 1) + return node + + +min_depth = 4 +max_depth = 16 +stretch_depth = max_depth + 1 + +print("stretch tree of depth %d\t check:" % + stretch_depth, CreateTreeNode(stretch_depth).check()) + +long_lived_tree = CreateTreeNode(max_depth) + +for depth in range(min_depth, stretch_depth, 2): + + check = 0 + iterations = 2**(max_depth - depth + min_depth) + for i in range(1, iterations + 1): + check += CreateTreeNode(depth).check() + + print("%d\t trees of depth %d\t check:" % (iterations, depth), check) + +print("long lived tree of depth %d\t check:" % + max_depth, long_lived_tree.check()) diff --git a/benchmark/mandelbrot.jvs b/benchmark/mandelbrot.jvs index 99f425798..c645cd7ec 100755 --- a/benchmark/mandelbrot.jvs +++ b/benchmark/mandelbrot.jvs @@ -73,6 +73,7 @@ public final class mandelbrot } System.out.println(); + System.out.flush(); } } diff --git a/benchmark/string.gbs b/benchmark/string1.gbs similarity index 100% rename from benchmark/string.gbs rename to benchmark/string1.gbs diff --git a/benchmark/string.jvs b/benchmark/string1.jvs similarity index 100% rename from benchmark/string.jvs rename to benchmark/string1.jvs diff --git a/benchmark/string.pl b/benchmark/string1.pl similarity index 100% rename from benchmark/string.pl rename to benchmark/string1.pl diff --git a/benchmark/string.py b/benchmark/string1.py similarity index 88% rename from benchmark/string.py rename to benchmark/string1.py index bcd4a721d..513706d47 100755 --- a/benchmark/string.py +++ b/benchmark/string1.py @@ -1,6 +1,5 @@ #!/usr/bin/python -import re import time import sys @@ -16,7 +15,7 @@ i = 0 while (i < imax+1000): i = i + 1 gstr += str - gstr = re.sub('efgh','____',gstr) + gstr = gstr.replace('efgh','____') lngth = len(gstr) if (lngth % (1024*64) == 0): print(int(time.time()-starttime),"sec\t\t",(lngth/1024),"kb")