The Computer Language
23.03 Benchmarks Game

binary-trees C# pgo #3 program

source code


namespace BenchmarkGameBTrees
{
    /*
          The Computer Language Benchmarks Game
          https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ 

          contributed by Marek Safar  
          optimized by kasthack
          *reset*
    */
    using System;
    using System.Threading;
    using System.Threading.Tasks;

    class BinaryTrees
    {
        const int minDepth = 4;
        public static void Main(String[] args)
        {
            int n = 0;
            if (args.Length > 0) n = Int32.Parse(args[0]);
            int maxDepth = Math.Max(minDepth + 2, n);
            int stretchDepth = maxDepth + 1;
            int check = (TreeNode.bottomUpTree(stretchDepth)).itemCheck();
            Console.WriteLine("stretch tree of depth {0}\t check: {1}", stretchDepth, check);
            TreeNode longLivedTree = TreeNode.bottomUpTree(maxDepth);
            for (int depth = minDepth; depth <= maxDepth; depth += 2)
            {
                int iterations = 1 << (maxDepth - depth + minDepth);
                check = 0;

                Parallel.For(1, iterations + 1,
                    () => 0,
                    (i, loop, localCheck) =>
                    {
                        return localCheck + 
                            (TreeNode.bottomUpTree(depth)).itemCheck();
                    },
                    localCheck => Interlocked.Add(ref check, localCheck)
                );

                Console.WriteLine("{0}\t trees of depth {1}\t check: {2}",
                    iterations, depth, check);
            }
            Console.WriteLine("long lived tree of depth {0}\t check: {1}",
                maxDepth, longLivedTree.itemCheck());
        }

        class TreeNode
        {
            private TreeNode left, right;

            internal static TreeNode bottomUpTree(int depth)
            {
                TreeNode t;
                ChildTreeNodes(out t, depth);
                return t;
            }
            static void ChildTreeNodes(out TreeNode node, int depth)
            {
                node = new TreeNode();
                if (depth > 0)
                {
                    ChildTreeNodes(out node.left, depth - 1);
                    ChildTreeNodes(out node.right, depth - 1);
                }
            }
            internal int itemCheck()
            {
                if (right == null) return 1;
                else return 1 + left.itemCheck() + right.itemCheck();
            }
        }
    }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
.NET SDK 7.0.200
Host Version: 7.0.3
Commit: 0a2bda10e8
<ServerGarbageCollection>true
<TieredPGO>true



Sun, 19 Feb 2023 20:15:11 GMT

MAKE:
cp binarytrees.csharppgo-3.csharppgo Program.cs
cp Include/csharppgo/tmp.csproj .
mkdir obj
cp Include/csharppgo/project.assets.json ./obj
~/dotnet/dotnet build -c Release --no-restore --no-self-contained -r ubuntu-x64 
MSBuild version 17.5.0-preview-23061-01+040e2a90e for .NET
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/Program.cs(52,30): warning CS8618: Non-nullable field 'left' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/tmp.csproj]
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/Program.cs(52,36): warning CS8618: Non-nullable field 'right' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/tmp.csproj]
  tmp -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/bin/Release/net7.0/ubuntu-x64/tmp.dll

Build succeeded.

/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/Program.cs(52,30): warning CS8618: Non-nullable field 'left' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/tmp.csproj]
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/Program.cs(52,36): warning CS8618: Non-nullable field 'right' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/tmp.csproj]
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.54

5.35s to complete and log all make actions

COMMAND LINE:
./bin/Release/net7.0/ubuntu-x64/tmp 21

PROGRAM OUTPUT:
stretch tree of depth 22	 check: 8388607
2097152	 trees of depth 4	 check: 65011712
524288	 trees of depth 6	 check: 66584576
131072	 trees of depth 8	 check: 66977792
32768	 trees of depth 10	 check: 67076096
8192	 trees of depth 12	 check: 67100672
2048	 trees of depth 14	 check: 67106816
512	 trees of depth 16	 check: 67108352
128	 trees of depth 18	 check: 67108736
32	 trees of depth 20	 check: 67108832
long lived tree of depth 21	 check: 4194303