Advertisement
NPSF3000

Grid Vs Lists

Dec 29th, 2011
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. namespace Subscription___GridVsLists
  8. {
  9.     class Program
  10.     {
  11.         static Random rnd = new Random(42);
  12.  
  13.         /*Dict Setings
  14.         const int its = 100000;
  15.         const int maxID = 1000;
  16.         const int ids = 200;
  17.         */
  18.  
  19.         //Array Setings
  20.         const int its = 100000;
  21.         const int maxID = 100;
  22.         const int ids = 10;
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             while (true)
  27.             {
  28.                 List<int> randomIDs = new List<int>();
  29.  
  30.                 for (int i = 0; i < 200; i++)
  31.                 {
  32.                     randomIDs.Add(rnd.Next(maxID));
  33.                 }
  34.  
  35.                 var dic = new Dictionary<int, int>(maxID);
  36.                 var arr = new int[maxID];
  37.                 var list = new SortedList<int, int>(maxID);
  38.  
  39.                 foreach (var id in randomIDs)
  40.                 {
  41.                     var value = rnd.Next();
  42.                     dic[id] = value;
  43.                     arr[id] = value;
  44.                     list[id] = value;
  45.                 }
  46.  
  47.                 var sw1 = Stopwatch.StartNew();
  48.                 long sumDic = 0;
  49.                 for (int i = 0; i < its; i++)
  50.                 {
  51.                     foreach (var id in dic)
  52.                     {
  53.                         sumDic += id.Value;
  54.                     }
  55.                 }
  56.                 sw1.Stop();
  57.  
  58.                 var sw2 = Stopwatch.StartNew();
  59.                 long sumArr = 0;
  60.                 for (int i = 0; i < its; i++)
  61.                 {
  62.                     foreach (var value in arr)
  63.                     {
  64.                         sumArr += value;
  65.                     }
  66.                 }
  67.                 sw2.Stop();
  68.  
  69.                 var sw3 = Stopwatch.StartNew();
  70.                 long sumList = 0;
  71.                 for (int i = 0; i < its; i++)
  72.                 {
  73.                     foreach (var value in list)
  74.                     {
  75.                         sumList += value.Value;
  76.                     }
  77.                 }
  78.                 sw3.Stop();
  79.  
  80.                 Console.WriteLine("Arr: count - {0} in {1}ms", sumArr, sw2.ElapsedMilliseconds);
  81.  
  82.                 Console.WriteLine("Dic: count - {0} in {1}ms", sumDic, sw1.ElapsedMilliseconds);
  83.  
  84.                 Console.WriteLine("List: count - {0} in {1}ms", sumList, sw3.ElapsedMilliseconds);
  85.  
  86.                 Console.ReadLine();
  87.             }
  88.         }
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement