/// <summary> /// Initializes a new instance of the <see cref="Hashtable<Key, Data>"/> class. /// </summary> /// <param name="initialCapacity">The initial capacity of the table.</param> public Hashtable(int initialCapacity) { if (initialCapacity < 1) { throw new ArgumentOutOfRangeException("initialCapacity", "cannot be < 1"); } array = new Hashtable <TKey, TData> .Node[initialCapacity]; writeLock = new OneManyLock(); }
public static void Go() { Int32 x = 0; const Int32 iterations = 10000000; // 10 million // How long does it take to increment x 10 million times // adding the overhead of calling an uncontended SimpleHybridLock? var shl = new SimpleHybridLock(); shl.Enter(); x++; shl.Leave(); Stopwatch sw = Stopwatch.StartNew(); for (Int32 i = 0; i < iterations; i++) { shl.Enter(); x++; shl.Leave(); } Console.WriteLine("Incrementing x in SimpleHybridLock: {0:N0}", sw.ElapsedMilliseconds); // How long does it take to increment x 10 million times // adding the overhead of calling an uncontended ANotherHybridLock? using (var ahl = new AnotherHybridLock()) { ahl.Enter(); x++; ahl.Leave(); sw.Restart(); for (Int32 i = 0; i < iterations; i++) { ahl.Enter(); x++; ahl.Leave(); } Console.WriteLine("Incrementing x in AnotherHybridLock: {0:N0}", sw.ElapsedMilliseconds); } using (var oml = new OneManyLock()) { oml.Enter(true); x++; oml.Leave(); sw.Restart(); for (Int32 i = 0; i < iterations; i++) { oml.Enter(true); x++; oml.Leave(); } Console.WriteLine("Incrementing x in OneManyLock: {0:N0}", sw.ElapsedMilliseconds); } using (var rwls = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion)) { rwls.EnterReadLock(); x++; rwls.ExitReadLock(); sw.Restart(); for (Int32 i = 0; i < iterations; i++) { rwls.EnterReadLock(); x++; rwls.ExitReadLock(); } Console.WriteLine("Incrementing x in ReaderWriterLockSlim: {0:N0}", sw.ElapsedMilliseconds); } var rwl = new ReaderWriterLock(); rwl.AcquireReaderLock(Timeout.Infinite); x++; rwl.ReleaseReaderLock(); sw.Restart(); for (Int32 i = 0; i < iterations; i++) { rwl.AcquireReaderLock(Timeout.Infinite); x++; rwl.ReleaseReaderLock(); } Console.WriteLine("Incrementing x in ReaderWriterLock: {0:N0}", sw.ElapsedMilliseconds); Object l = new Object(); Monitor.Enter(l); x++; Monitor.Exit(l); sw.Restart(); for (Int32 i = 0; i < iterations; i++) { Monitor.Enter(l); x++; Monitor.Exit(l); } Console.WriteLine("Incrementing x in Monitor: {0:N0}", sw.ElapsedMilliseconds); sw.Restart(); for (Int32 i = 0; i < iterations; i++) { lock (l) { x++; } } Console.WriteLine("Incrementing x in lock: {0:N0}", sw.ElapsedMilliseconds); Console.ReadLine(); }