Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace TraversalWithYield
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Tuple<double, double>> benchmark = new List<Tuple<double, double>>();
- Node<int> rootNode = CreateNodes();
- List<int> results = new List<int>(41);
- results.Clear();
- for (int z = 0; z < 100000; z++)
- {
- //Test1
- DateTime now = DateTime.Now;
- foreach (int i in DepthFirstTraversal.Traverse<int>(rootNode))
- {
- results.Add(i);
- }
- double r1 = (DateTime.Now - now).TotalMilliseconds;
- results.Clear();
- //Test2
- now = DateTime.Now;
- DepthFirstTraversal.Traverse<int>(rootNode, (i) => results.Add(i));
- double r2 = (DateTime.Now - now).TotalMilliseconds;
- Tuple<double, double> time = new Tuple<double, double>(r1, r2);
- benchmark.Add(time);
- }
- double avgA = 0, avgB = 0;
- foreach (Tuple<double, double> t in benchmark)
- {
- avgA += t.Item1;
- avgB += t.Item2;
- }
- avgA /= benchmark.Count;
- avgB /= benchmark.Count;
- Console.Out.WriteLine(String.Format("{0} iterations, yield performance {1}ms, Action<> performance {2}ms", benchmark.Count, avgA, avgB));
- Console.ReadLine();
- }
- private static Node<int> CreateNodes()
- {
- List<Node<int>> list = new List<Node<int>>();
- int curDepth = depth;
- if(depth < 20)
- {
- ++depth;
- list.Add(CreateNodes());
- ++depth;
- list.Add(CreateNodes());
- }
- Node<int> node = new Node<int>(curDepth, list);
- return node;
- }
- private static int depth = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement