Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Program
- {
- [STAThread]
- static void Main()
- {
- int times = 100000;
- Console.WriteLine("Testing performance of adding " + times + " integers to different data structures...");
- Console.WriteLine();
- Random r = new Random();
- List<int> optimisedL = new List<int>();
- SkipList<int> list = new SkipList<int>();
- AvlTree<int, int> tree = new AvlTree<int, int>();
- Stopwatch watch = new Stopwatch();
- watch.Start();
- for (int i = 0; i < times; i++)
- {
- int rand = r.Next(100000);
- AddToSortedList(optimisedL, rand);
- }
- watch.Stop();
- double listTime = watch.Elapsed.TotalSeconds;
- Console.WriteLine("Sorted list: " + listTime);
- watch.Reset();
- watch.Start();
- for (int i = 0; i < times; i++)
- {
- int rand = r.Next(100000);
- list.Add(rand);
- }
- watch.Stop();
- double skipListTime = watch.Elapsed.TotalSeconds;
- Console.WriteLine("Skip list: "+skipListTime);
- watch.Reset();
- watch.Start();
- for (int i = 0; i < times; i++)
- {
- int rand = r.Next(100000);
- tree.Insert(rand, rand);
- }
- watch.Stop();
- double treeTime = watch.Elapsed.TotalSeconds;
- Console.WriteLine("AVL tree: "+treeTime);
- Console.WriteLine("DONE");
- Console.ReadKey();
- }
- private static void AddToSortedList(List<int> sortedList, int item)
- {
- int min = 0;
- int max = sortedList.Count;
- int index = 0;
- while (min < max)
- {
- index = (min + max) / 2;
- if (item < sortedList[index])
- {
- min = index + 1;
- index = min;
- }
- else
- {
- if (item > sortedList[index])
- {
- max = index;
- index = max;
- }
- else
- break;
- }
- }
- sortedList.Insert(index, item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement