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;
- using System.Diagnostics;
- namespace Subscription___GridVsLists
- {
- class Program
- {
- static Random rnd = new Random(42);
- /*Dict Setings
- const int its = 100000;
- const int maxID = 1000;
- const int ids = 200;
- */
- //Array Setings
- const int its = 100000;
- const int maxID = 100;
- const int ids = 10;
- static void Main(string[] args)
- {
- while (true)
- {
- List<int> randomIDs = new List<int>();
- for (int i = 0; i < 200; i++)
- {
- randomIDs.Add(rnd.Next(maxID));
- }
- var dic = new Dictionary<int, int>(maxID);
- var arr = new int[maxID];
- var list = new SortedList<int, int>(maxID);
- foreach (var id in randomIDs)
- {
- var value = rnd.Next();
- dic[id] = value;
- arr[id] = value;
- list[id] = value;
- }
- var sw1 = Stopwatch.StartNew();
- long sumDic = 0;
- for (int i = 0; i < its; i++)
- {
- foreach (var id in dic)
- {
- sumDic += id.Value;
- }
- }
- sw1.Stop();
- var sw2 = Stopwatch.StartNew();
- long sumArr = 0;
- for (int i = 0; i < its; i++)
- {
- foreach (var value in arr)
- {
- sumArr += value;
- }
- }
- sw2.Stop();
- var sw3 = Stopwatch.StartNew();
- long sumList = 0;
- for (int i = 0; i < its; i++)
- {
- foreach (var value in list)
- {
- sumList += value.Value;
- }
- }
- sw3.Stop();
- Console.WriteLine("Arr: count - {0} in {1}ms", sumArr, sw2.ElapsedMilliseconds);
- Console.WriteLine("Dic: count - {0} in {1}ms", sumDic, sw1.ElapsedMilliseconds);
- Console.WriteLine("List: count - {0} in {1}ms", sumList, sw3.ElapsedMilliseconds);
- Console.ReadLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement