Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- namespace ConsoleTests
- {
- class Program
- {
- static void Main(string[] args)
- {
- Random rnd = new Random((int)DateTime.Now.Ticks);
- MyList<int> listType = new MyList<int>();
- MyCoolType<int> coolType = new MyCoolType<int>();
- Stopwatch sw = new Stopwatch();
- sw.Start();
- Console.WriteLine("Inserting into MyList<int>");
- for (int i = 0; i < 1000000; i++)
- {
- int idx = rnd.Next(0, 2000000000);
- listType[idx] = rnd.Next();
- }
- sw.Stop();
- Console.WriteLine("Insertion in MyList<int> took " + sw.Elapsed.TotalSeconds + " seconds");
- sw.Reset();
- sw.Start();
- Console.WriteLine("Inserting into CoolType<int>");
- for (int i = 0; i < 1000000; i++)
- {
- int idx = rnd.Next(0, int.MaxValue);
- coolType[idx] = rnd.Next();
- }
- sw.Stop();
- Console.WriteLine("Insertion in MyList<int> took " + sw.Elapsed.TotalSeconds + " seconds");
- sw.Reset();
- Console.WriteLine("Looking up 1,000,000 random elements in MyList<int>");
- sw.Start();
- int x = 0;
- for (int i = 0; i < 10000000; i++)
- {
- int idx = rnd.Next(0, 2000000000);
- x = listType[idx];
- }
- sw.Stop();
- Console.WriteLine("Looking up 1,000,000 random elements in MyList<int> took " + sw.Elapsed.TotalSeconds + " seconds");
- sw.Reset();
- Console.WriteLine("Looking up 1,000,000 random elements in MyList<int>");
- sw.Start();
- for (int i = 0; i < 10000000; i++)
- {
- int idx = rnd.Next(0, int.MaxValue);
- bool found = coolType.TryGetValue(idx, out x);
- }
- sw.Stop();
- Console.WriteLine("Looking up 1,000,000 random elements in CoolType<int> took " + sw.Elapsed.TotalSeconds + " seconds");
- Console.ReadLine();
- }
- }
- public class MyCoolType<T> : Dictionary<int, T> { }
- public class MyList<T> : List<T>
- {
- public T this[int i]
- {
- get
- {
- while (i >= this.Count) this.Add(default(T));
- return base[i];
- }
- set
- {
- while (i >= this.Count) this.Add(default(T));
- base[i] = value;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement