Advertisement
Guest User

Data structure performance test

a guest
Apr 3rd, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. static class Program
  2. {
  3.     [STAThread]
  4.     static void Main()
  5.     {
  6.         int times = 100000;
  7.         Console.WriteLine("Testing performance of adding " + times + " integers to different data structures...");
  8.         Console.WriteLine();
  9.         Random r = new Random();
  10.         List<int> optimisedL = new List<int>();
  11.         SkipList<int> list = new SkipList<int>();
  12.         AvlTree<int, int> tree = new AvlTree<int, int>();
  13.         Stopwatch watch = new Stopwatch();
  14.         watch.Start();
  15.         for (int i = 0; i < times; i++)
  16.         {
  17.             int rand = r.Next(100000);
  18.             AddToSortedList(optimisedL, rand);
  19.         }
  20.         watch.Stop();
  21.         double listTime = watch.Elapsed.TotalSeconds;
  22.         Console.WriteLine("Sorted list: " + listTime);
  23.         watch.Reset();
  24.         watch.Start();
  25.         for (int i = 0; i < times; i++)
  26.         {
  27.             int rand = r.Next(100000);
  28.             list.Add(rand);
  29.         }
  30.         watch.Stop();
  31.         double skipListTime = watch.Elapsed.TotalSeconds;
  32.         Console.WriteLine("Skip list:   "+skipListTime);
  33.         watch.Reset();
  34.         watch.Start();
  35.         for (int i = 0; i < times; i++)
  36.         {
  37.             int rand = r.Next(100000);
  38.             tree.Insert(rand, rand);
  39.         }
  40.         watch.Stop();
  41.         double treeTime = watch.Elapsed.TotalSeconds;
  42.         Console.WriteLine("AVL tree:    "+treeTime);
  43.  
  44.         Console.WriteLine("DONE");
  45.         Console.ReadKey();
  46.     }
  47.  
  48.     private static void AddToSortedList(List<int> sortedList, int item)
  49.     {
  50.         int min = 0;
  51.         int max = sortedList.Count;
  52.         int index = 0;
  53.         while (min < max)
  54.         {
  55.             index = (min + max) / 2;
  56.             if (item < sortedList[index])
  57.             {
  58.                 min = index + 1;
  59.                 index = min;
  60.             }
  61.             else
  62.             {
  63.                 if (item > sortedList[index])
  64.                 {
  65.                     max = index;
  66.                     index = max;
  67.                 }
  68.                 else
  69.                     break;
  70.             }
  71.         }
  72.         sortedList.Insert(index, item);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement