public void TestFastDictionary() { var facit = new Dictionary <int, string>(); var fast = new FastDictionary <int, string>(); for (int r = 0; r < 1000000; r++) { var rnd = PRNG.Next(0, 13); switch (rnd) { case 0: facit.Clear(); fast.Clear(); break; case 1: case 2: { // add/change using indexer int key; if (facit.Count > 2 && PRNG.Next(0, 100) < 20) { key = GetRandomKey(facit); } else { key = PRNG.Next(1, 1000); } facit[key] = key.ToString(); fast[key] = key.ToString(); break; } case 3: case 4: // add using Add() int b = PRNG.Next(1, 1000); if (facit.ContainsKey(b) == false) { facit.Add(b, b.ToString()); fast.Add(b, b.ToString()); } break; case 5: case 6: { // add using GetOrInit() int c = PRNG.Next(0, 1000); bool exists = facit.ContainsKey(c); ref var str = ref fast.GetOrInit(c, out bool wasCreated); Assert.AreEqual(exists, !wasCreated); str = c.ToString(); if (!exists) { facit[c] = c.ToString(); } break; }
public void CompareSCGAndFastDictionaryWithSymbol() { var dictionary = new Dictionary <Symbol, int>(); var fastDictionary = new FastDictionary <Symbol, int>(); var symbols = new Symbol[1000]; for (int i = 0; i < 1000; i++) { var s = new Symbol(i.ToString()); symbols[i] = s; dictionary.Add(s, i); fastDictionary.Add(s, i); } const int count = 10000; for (int r = 0; r < 10; r++) { var sum = 0L; using (Benchmark.Run("Dictionary", count * 1000)) { for (int i = 0; i < count; i++) { for (int j = 0; j < 1000; j++) { sum += dictionary[symbols[j]]; } } } Assert.True(sum > 0); var sum1 = 0L; using (Benchmark.Run("FastDictionary", count * 1000)) { for (int i = 0; i < count; i++) { for (int j = 0; j < 1000; j++) { sum1 += fastDictionary[symbols[j]]; } } } Assert.True(sum > 0); Assert.AreEqual(sum, sum1); } Benchmark.Dump(); }
public unsafe void CompareSCGAndFastDictionaryWithSymbol() { var d = new Dictionary <Symbol, int>(); var fd = new FastDictionary <Symbol, int>(); var symbols = new Symbol[1000]; for (int i = 0; i < 1000; i++) { var s = new Symbol(i.ToString()); symbols[i] = s; d.Add(s, i); fd.Add(s, i); } const int count = 10000; var sw = new Stopwatch(); for (int r = 0; r < 10; r++) { var sum = 0L; sw.Restart(); for (int i = 0; i < count; i++) { for (int j = 0; j < 1000; j++) { sum += d[symbols[j]]; } } sw.Stop(); Console.WriteLine($"Dictionary {sw.ElapsedMilliseconds}"); Assert.True(sum > 0); sum = 0L; sw.Restart(); for (int i = 0; i < count; i++) { for (int j = 0; j < 1000; j++) { sum += fd[symbols[j]]; } } sw.Stop(); Console.WriteLine($"FastDictionary {sw.ElapsedMilliseconds}"); Assert.True(sum > 0); } }
/// <summary> /// Register a system /// </summary> /// <param name="system"></param> /// <typeparam name="T"></typeparam> public void RegisterSystem <T>(T system) { if (!m_IsDynamicSystemId && !m_IsAddingToFixedCollection) { if (!IdToSystems.ContainsValue(system)) { Debug.LogError($"We couldn't add {system} since it use a fixed system list."); return; } } var id = (uint)IdToSystems.Count; IdToSystems.Add(id, system); SystemsToId.Add(system, id); }
public unsafe void CompareSCGAndFastDictionaryWithInts() { var d = new Dictionary <int, int>(); var fd = new FastDictionary <int, int>(); for (int i = 0; i < 1000; i++) { d.Add(i, i); fd.Add(i, i); } const int count = 100000; var sw = new Stopwatch(); for (int r = 0; r < 10; r++) { var sum = 0L; sw.Restart(); for (int i = 0; i < count; i++) { for (int j = 0; j < 1000; j++) { sum += d[j]; } } sw.Stop(); Console.WriteLine($"Dictionary {sw.ElapsedMilliseconds}"); Assert.True(sum > 0); var sum1 = 0L; sw.Restart(); for (int i = 0; i < count; i++) { for (int j = 0; j < 1000; j++) { sum1 += fd[j]; } } sw.Stop(); Console.WriteLine($"FastDictionary {sw.ElapsedMilliseconds}"); Assert.True(sum > 0); Assert.AreEqual(sum, sum1); } }
public void CompareSCGAndFastDictionaryWithInts() { var dictionary = new Dictionary <long, long>(); var dictionarySlim = new DictionarySlim <long, long>(); var fastDictionary = new FastDictionary <long, long>(); var concurrentDictionary = new ConcurrentDictionary <long, long>(); var length = 10000; for (int i = 0; i < length; i++) { dictionary.Add(i, i); ref var x = ref dictionarySlim.GetOrAddValueRef(i); x = i; fastDictionary.Add(i, i); concurrentDictionary[i] = i; }
public void CompareSCGAndFastDictionaryWithInts() { var dictionary = new Dictionary <int, int>(); var fastDictionary = new FastDictionary <int, int>(); for (int i = 0; i < 1000; i++) { dictionary.Add(i, i); fastDictionary.Add(i, i); } const int count = 50000; for (int r = 0; r < 10; r++) { var sum = 0L; using (Benchmark.Run("Dictionary", count * 1000)) { for (int i = 0; i < count; i++) { for (int j = 0; j < 1000; j++) { sum += dictionary[j]; } } } Assert.True(sum > 0); var sum1 = 0L; using (Benchmark.Run("FastDictionary", count * 1000)) { for (int i = 0; i < count; i++) { for (int j = 0; j < 1000; j++) { sum1 += fastDictionary[j]; } } } Assert.True(sum > 0); Assert.AreEqual(sum, sum1); } Benchmark.Dump(); }
dynamic IRefactorEntity.CreateNode(object node) { Parent.EnsureSchemaMigration(); DynamicEntity entity = new DynamicEntity(this, Parser.ShouldExecute, node); object?key = entity.GetKey(); if (key is null) { return(entity); } if (staticData.ContainsKey(key)) { throw new PersistenceException(string.Format("A static entity with the same key already exists.")); } staticData.Add(key, entity); return(entity); }
public void WeakReferenceLookup() { // How bad is WR lookup var d = new FastDictionary <long, object>(); // var ds = new DictionarySlim<long, object>(); var lockedWeakDictionary = new LockedWeakDictionary <long>(); var indexedLockedWeakDictionary = new IndexedLockedWeakDictionary <long, Dummy>(); var cd = new ConcurrentDictionary <long, object>(); var wcd = new ConcurrentDictionary <long, WeakReference <object> >(); //var wcd2 = new ConcurrentDictionary<long, WeakReference>(); var wcd3 = new ConcurrentDictionary <long, GCHandle>(); var locker = 0L; var count = 1_000; for (int i = 0; i < count; i++) { var obj = (object)new Dummy(); d.Add(i, obj); // ds.GetOrAddValueRef(i) = obj; cd.TryAdd(i, obj); wcd.TryAdd(i, new WeakReference <object>(obj)); //wcd2.TryAdd(i, new WeakReference(obj)); var h = GCHandle.Alloc(obj, GCHandleType.Weak); wcd3.TryAdd(i, h); lockedWeakDictionary.TryAdd(i, obj); // lockedWeakDictionary.TryAdd(i, obj); } var mult = 100_000; for (int r = 0; r < 10; r++) { //var sum1 = 0.0; //using (Benchmark.Run("CD", count * mult)) //{ // for (int i = 0; i < count * mult; i++) // { // if (cd.TryGetValue(i / mult, out var obj)) // { // //sum1 += (int)obj; // } // else // { // Assert.Fail(); // } // } //} //var sum2 = 0.0; //using (Benchmark.Run("WCD", count * mult)) //{ // // Not so bad, this just needs to save something more in other places // for (int i = 0; i < count * mult; i++) // { // wcd.TryGetValue(i / mult, out var wr); // if (wr.TryGetTarget(out var tgt)) // { // //sum2 += (int)tgt; // } // else // { // Assert.Fail(); // } // } //} //Assert.AreEqual(sum1, sum2); //var sum4 = 0.0; //using (Benchmark.Run("WCD2", count * mult)) //{ // // Not so bad, this just needs to save something more in other places // for (int i = 0; i < count * mult; i++) // { // wcd2.TryGetValue(i / mult, out var wr2); // if (wr2.Target != null) // { // sum4 += (int)wr2.Target; // } // else // { // Assert.Fail(); // } // } //} //Assert.AreEqual(sum1, sum4); //var sum5 = 0.0; WCD_H(count, mult, wcd3); // Assert.AreEqual(sum1, sum5); //var sum3 = 0.0; //using (Benchmark.Run("LD", count * mult)) //{ // for (int i = 0; i < count * mult; i++) // { // lock (d) // { // if (d.TryGetValue(i / mult, out var obj)) // { // sum3 += (int)obj; // } // else // { // Assert.Fail(); // } // } // } //} var sum6 = 0.0; LWD(count, mult, lockedWeakDictionary); //var sum7 = 0.0; //using (Benchmark.Run("ILWD", count * mult)) //{ // for (int i = 0; i < count * mult; i++) // { // if (indexedLockedWeakDictionary.TryGetValue(i / mult, out var val)) // { // //sum7 += (int)val.StorageIndex; // } // else // { // Assert.Fail(); // } // } //} } Benchmark.Dump(); Console.WriteLine(d.Count); }
public void WeakReferenceLookup() { // How bad is WR lookup var d = new FastDictionary <long, object>(); var lockedWeakDictionary = new LockedWeakDictionary <long>(); var cd = new ConcurrentDictionary <long, object>(); var wcd = new ConcurrentDictionary <long, WeakReference <object> >(); //var wcd2 = new ConcurrentDictionary<long, WeakReference>(); var wcd3 = new ConcurrentDictionary <long, GCHandle>(); var locker = 0L; var count = 1_000; for (int i = 0; i < count; i++) { var obj = (object)i; d.Add(i, obj); cd.TryAdd(i, obj); wcd.TryAdd(i, new WeakReference <object>(obj)); //wcd2.TryAdd(i, new WeakReference(obj)); var h = GCHandle.Alloc(obj, GCHandleType.Weak); wcd3.TryAdd(i, h); lockedWeakDictionary.TryAdd(i, obj); } var mult = 100_000; var sum1 = 0.0; using (Benchmark.Run("CD", count * mult)) { for (int i = 0; i < count * mult; i++) { if (cd.TryGetValue(i / mult, out var obj)) { sum1 += (int)obj; } else { Assert.Fail(); } } } var sum2 = 0.0; using (Benchmark.Run("WCD", count * mult)) { // Not so bad, this just needs to save something more in other places for (int i = 0; i < count * mult; i++) { wcd.TryGetValue(i / mult, out var wr); if (wr.TryGetTarget(out var tgt)) { sum2 += (int)tgt; } else { Assert.Fail(); } } } Assert.AreEqual(sum1, sum2); //var sum4 = 0.0; //using (Benchmark.Run("WCD2", count * mult)) //{ // // Not so bad, this just needs to save something more in other places // for (int i = 0; i < count * mult; i++) // { // wcd2.TryGetValue(i / mult, out var wr2); // if (wr2.Target != null) // { // sum4 += (int)wr2.Target; // } // else // { // Assert.Fail(); // } // } //} //Assert.AreEqual(sum1, sum4); var sum5 = 0.0; using (Benchmark.Run("WCD H", count * mult)) { // Not so bad, this just needs to save something more in other places for (int i = 0; i < count * mult; i++) { wcd3.TryGetValue(i / mult, out var wr2); if (wr2.Target is int val) { sum5 += val; } //else //{ // Assert.Fail(); //} } } Assert.AreEqual(sum1, sum5); //var sum3 = 0.0; //using (Benchmark.Run("LD", count * mult)) //{ // for (int i = 0; i < count * mult; i++) // { // lock (d) // { // if (d.TryGetValue(i / mult, out var obj)) // { // sum3 += (int)obj; // } // else // { // Assert.Fail(); // } // } // } //} var sum6 = 0.0; using (Benchmark.Run("LWD", count * mult)) { for (int i = 0; i < count * mult; i++) { if (lockedWeakDictionary.TryGetValue(i / mult, out var val)) { sum6 += (int)val; } } } Benchmark.Dump(); Console.WriteLine(d.Count); }