mquinlan

FlattenTest

Mar 26th, 2025 (edited)
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. #if DEBUG
  2. using System.Diagnostics;
  3. #endif
  4. using BenchmarkDotNet.Attributes;
  5. #if DEBUG
  6. #else
  7. using BenchmarkDotNet.Running;
  8. #endif
  9.  
  10. namespace FlattenTest;
  11.  
  12. internal class Node(IEnumerable<Node>? children)
  13. {
  14.     public IEnumerable<Node>? Children { get; } = children;
  15.  
  16.     public IEnumerable<Node> GetAllChildrenConcat()
  17.         => new[] { this }.Concat(Children?.SelectMany(child => child.GetAllChildrenConcat()) ?? []);
  18.    
  19.     public IEnumerable<Node> GetAllChildrenRange()
  20.         => [this, ..Children?.SelectMany(child => child.GetAllChildrenRange()) ?? []];
  21.  
  22.     public IEnumerable<Node> GetAllChildrenRecursive()
  23.     {
  24.         yield return this;
  25.         if (Children is not null)
  26.         {
  27.             foreach (var child in Children)
  28.             {
  29.                 foreach (var node in child.GetAllChildrenRecursive()) yield return node;
  30.             }
  31.         }
  32.     }
  33.  
  34.     public IEnumerable<Node> GetAllChildrenNonRecursive()
  35.     {
  36.         yield return this;
  37.         if (Children is not null)
  38.         {
  39.             var stack = new Stack<IEnumerable<Node>>();
  40.             stack.Push(Children);
  41.             while (stack.Count != 0)
  42.             {
  43.                 var children = stack.Pop();
  44.                 foreach (var child in children)
  45.                 {
  46.                     yield return child;
  47.                     if (child.Children is not null) stack.Push(child.Children);
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. public class Program
  55. {
  56.     private const int Depth = 8;
  57.     private const int ChildrenCount = 8;
  58.     private static Node root = null!;
  59.  
  60.     [GlobalSetup]
  61.     public static void Setup()
  62.     {
  63.         root = BuildNode(0);
  64.  
  65.         static Node BuildNode(int depth)
  66.         {
  67.             if (depth >= Depth) return new Node(null);
  68.             var children = new List<Node>(ChildrenCount);
  69.             for (var i = 0; i < ChildrenCount; i++)
  70.             {
  71.                 children.Add(BuildNode(depth + 1));
  72.             }
  73.             return new Node(children);
  74.         }
  75.     }
  76.    
  77.     [Benchmark]
  78.     public int ConcatTest() => root.GetAllChildrenConcat().Count();
  79.  
  80.     [Benchmark]
  81.     public int RangeTest() => root.GetAllChildrenRange().Count();
  82.  
  83.     [Benchmark]
  84.     public int RecursiveTest() => root.GetAllChildrenRecursive().Count();
  85.  
  86.     [Benchmark]
  87.     public int NonRecursiveTest() => root.GetAllChildrenNonRecursive().Count();
  88.  
  89.     static void Main()
  90.     {
  91. #if DEBUG
  92.         var program = new Program();
  93.         Setup();
  94.         var concatCount = program.ConcatTest();
  95.         var rangeCount = program.RangeTest();
  96.         var recursiveCount = program.RecursiveTest();
  97.         var nonRecursiveCount = program.NonRecursiveTest();
  98.         Console.WriteLine(concatCount);
  99.         Debug.Assert(concatCount == rangeCount);
  100.         Debug.Assert(concatCount == recursiveCount);
  101.         Debug.Assert(concatCount == nonRecursiveCount);
  102. #else        
  103.         _ = BenchmarkRunner.Run<Program>();
  104. #endif
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment