Advertisement
Guest User

Untitled

a guest
Feb 12th, 2011
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace TraversalWithYield
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             List<Tuple<double, double>> benchmark = new List<Tuple<double, double>>();
  13.  
  14.  
  15.             Node<int> rootNode = CreateNodes();
  16.             List<int> results = new List<int>(41);
  17.  
  18.             results.Clear();
  19.             for (int z = 0; z < 100000; z++)
  20.             {
  21.                 //Test1            
  22.                 DateTime now = DateTime.Now;
  23.                 foreach (int i in DepthFirstTraversal.Traverse<int>(rootNode))
  24.                 {
  25.                     results.Add(i);
  26.                 }
  27.                 double r1 = (DateTime.Now - now).TotalMilliseconds;
  28.  
  29.                 results.Clear();
  30.                 //Test2
  31.                 now = DateTime.Now;
  32.                 DepthFirstTraversal.Traverse<int>(rootNode, (i) => results.Add(i));
  33.                 double r2 = (DateTime.Now - now).TotalMilliseconds;
  34.  
  35.                 Tuple<double, double> time = new Tuple<double, double>(r1, r2);
  36.                 benchmark.Add(time);
  37.             }
  38.  
  39.             double avgA = 0, avgB = 0;
  40.             foreach (Tuple<double, double> t in benchmark)
  41.             {
  42.                 avgA += t.Item1;
  43.                 avgB += t.Item2;
  44.             }
  45.  
  46.             avgA /= benchmark.Count;
  47.             avgB /= benchmark.Count;
  48.  
  49.             Console.Out.WriteLine(String.Format("{0} iterations, yield performance {1}ms, Action<> performance {2}ms", benchmark.Count, avgA, avgB));
  50.             Console.ReadLine();
  51.         }
  52.  
  53.  
  54.         private static Node<int> CreateNodes()
  55.         {
  56.             List<Node<int>> list = new List<Node<int>>();
  57.             int curDepth = depth;
  58.             if(depth < 20)
  59.             {
  60.                 ++depth;
  61.                 list.Add(CreateNodes());
  62.                 ++depth;
  63.                 list.Add(CreateNodes());                
  64.             }
  65.  
  66.             Node<int> node = new Node<int>(curDepth, list);
  67.             return node;            
  68.         }
  69.  
  70.         private static int depth = 0;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement