Advertisement
Guest User

Performance Test Sparse/Dense Random Object Storage

a guest
May 20th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. namespace ConsoleTests
  6. {
  7.  
  8.     class Program
  9.     {
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             Random rnd = new Random((int)DateTime.Now.Ticks);
  14.                      
  15.             MyList<int> listType = new MyList<int>();
  16.             MyCoolType<int> coolType = new MyCoolType<int>();
  17.            
  18.             Stopwatch sw = new Stopwatch();
  19.             sw.Start();
  20.  
  21.             Console.WriteLine("Inserting into MyList<int>");
  22.  
  23.             for (int i = 0; i < 1000000; i++)
  24.             {
  25.                 int idx = rnd.Next(0, 2000000000);
  26.                 listType[idx] = rnd.Next();
  27.             }
  28.            
  29.             sw.Stop();
  30.  
  31.             Console.WriteLine("Insertion in MyList<int> took " + sw.Elapsed.TotalSeconds + " seconds");
  32.  
  33.             sw.Reset();
  34.             sw.Start();
  35.             Console.WriteLine("Inserting into CoolType<int>");
  36.  
  37.             for (int i = 0; i < 1000000; i++)
  38.             {
  39.                 int idx = rnd.Next(0, int.MaxValue);
  40.                 coolType[idx] = rnd.Next();
  41.             }
  42.  
  43.             sw.Stop();
  44.  
  45.             Console.WriteLine("Insertion in MyList<int> took " + sw.Elapsed.TotalSeconds + " seconds");
  46.  
  47.             sw.Reset();
  48.  
  49.             Console.WriteLine("Looking up 1,000,000 random elements in MyList<int>");
  50.  
  51.             sw.Start();
  52.             int x = 0;
  53.            
  54.             for (int i = 0; i < 10000000; i++)
  55.             {
  56.                 int idx = rnd.Next(0, 2000000000);
  57.                 x = listType[idx];
  58.             }
  59.  
  60.             sw.Stop();
  61.             Console.WriteLine("Looking up 1,000,000 random elements in MyList<int> took " + sw.Elapsed.TotalSeconds + " seconds");
  62.  
  63.             sw.Reset();
  64.  
  65.             Console.WriteLine("Looking up 1,000,000 random elements in MyList<int>");
  66.  
  67.             sw.Start();
  68.  
  69.             for (int i = 0; i < 10000000; i++)
  70.             {
  71.                 int idx = rnd.Next(0, int.MaxValue);
  72.                 bool found = coolType.TryGetValue(idx, out x);
  73.             }
  74.  
  75.             sw.Stop();
  76.             Console.WriteLine("Looking up 1,000,000 random elements in CoolType<int> took " + sw.Elapsed.TotalSeconds + " seconds");
  77.            
  78.             Console.ReadLine();
  79.         }
  80.  
  81.     }
  82.  
  83.     public class MyCoolType<T> : Dictionary<int, T> { }
  84.  
  85.     public class MyList<T> : List<T>
  86.     {
  87.         public T this[int i]
  88.         {
  89.             get
  90.             {
  91.                 while (i >= this.Count) this.Add(default(T));
  92.                 return base[i];
  93.             }
  94.             set
  95.             {
  96.                 while (i >= this.Count) this.Add(default(T));
  97.                 base[i] = value;
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement