static void UnBalancedRangeInsert(btree bt, int[] range, bool recursv = false) { int constthread = 4; if (range.Length > (2 ^ 12) && !recursv) { for (int i = 2, count = 1; count < 5; i *= 2, count++) { for (int j = (range.Length / i); j < range.Length; j += (range.Length / i)) { bt.Insert(range[j]); } } System.Collections.Generic.List<Task> wh = new List<Task>(); for (int i = 1, cnt = 1; cnt <= constthread; i += (range.Length / constthread), cnt++) { int start = i; int end = (range.Length / constthread) * cnt; var t = new Task(() => { int[] rng = Enumerable.Range(start, end).ToArray(); UnBalancedRangeInsert(bt, rng, true); }); t.Start(); wh.Add(t); } //Task.WaitAll(wh.ToArray()); } else { for (int i = 1; i < range.Length; i++) { bt.Insert(range[i]); } } }
static void Main(string[] args) { for (int i = 0; i < 20; i++) { var rand = new Random(); LockHelper lockhelp = new MonitorLockHelper(); var sw = new System.Diagnostics.Stopwatch(); int[] range = Enumerable.Range(1, (int)Math.Pow(2.0, 23)).ToArray(); //sw.Restart(); //var btTestNoLookupSTRand = new btree(lockhelp); //RandomRangeInsert(btTestNoLookupSTRand, range, sw, true); //sw.Stop(); //Console.WriteLine("Random insert single threaded time {0}ms for {1} nodes", sw.Elapsed.TotalMilliseconds, (int)Math.Pow(2.0, 24)); //sw.Restart(); //var btTestNoLookupRand = new btree(lockhelp); //RandomRangeInsert(btTestNoLookupRand, range, sw); //Task.WaitAll(wh.ToArray()); //sw.Stop(); //wh.Clear(); //Console.WriteLine("Random insert multi threaded time {0}ms for {1} nodes", sw.Elapsed.TotalMilliseconds, (int)Math.Pow(2.0, 24)); //sw.Restart(); //var btTestNoLookupsST = new btree(lockhelp); //BalancedRangeInsert(btTestNoLookupsST, range, sw, true); //sw.Stop(); //Console.WriteLine("Balanced insert single threaded time {0}ms for {1} nodes", sw.Elapsed.TotalMilliseconds, (int)Math.Pow(2.0, 24)); //sw.Restart(); //var btTestNoLookups = new btree(lockhelp); //BalancedRangeInsert(btTestNoLookups, range, sw); //Task.WaitAll(wh.ToArray()); //sw.Stop(); //wh.Clear(); //Console.WriteLine("Balanced insert multi threaded time {0}ms for {1} nodes", sw.Elapsed.TotalMilliseconds, (int)Math.Pow(2.0, 24)); sw.Restart(); var bttest = new btree(lockhelp); BalancedRangeInsert(bttest, range, sw); var cts = new System.Threading.CancellationTokenSource(); var t = new Task(() => { bool contains = true; while (!cts.IsCancellationRequested) { contains &= bttest.Contains(rand.Next(1, (int)Math.Pow(2.0, 22))); System.Threading.Thread.Sleep(1); } }, cts.Token); var t2 = new Task(() => { while (!cts.IsCancellationRequested) { bttest.Remove(rand.Next(1, (int)Math.Pow(2.0, 22))); System.Threading.Thread.Sleep(1); } }, cts.Token); t2.Start(); Task.WaitAll(wh.ToArray()); sw.Stop(); cts.Cancel(); Console.WriteLine("Balanced insert multi threaded, w/ lookup and removal time: {0}", sw.Elapsed.TotalMilliseconds); } Console.ReadLine(); }
static void RandomRangeInsert(btree bt, int[] range, System.Diagnostics.Stopwatch sw, bool recursv = false) { int constthread = 4; if (range.Length > (2 ^ 8) && !recursv) { for (int i = 1, cnt = 1; cnt <= constthread; cnt++) { int start = i; int end = (range.Length / constthread) * cnt; i = end; var t = new Task(() => { int[] rng = Enumerable.Range(start, end - start).ToArray(); RandomRangeInsert(bt, rng, sw, true); }); t.Start(); wh.Add(t); } } else { var sw2 = new System.Diagnostics.Stopwatch(); var rand = new Random(); for (int j = 0; j < range.Length; j++) { int rnd = rand.Next(range.First(), range.Last()); sw2.Start(); bt.Insert(rnd); sw2.Stop(); } //Console.WriteLine("Time taken to randomly insert {0} nodes: {1}ms", range.Length, sw2.Elapsed.TotalMilliseconds); } }
static void BalancedRangeInsert(btree bt, int[] range, System.Diagnostics.Stopwatch sw, bool recursv = false) { int constthread = 4; if (range.Length > (2 ^ 8) && !recursv) { for (int i = 2, count = 1; count < 5; i *= 2, count++) { for (int j = (range.Length / i); j < range.Length; j += (range.Length / i)) { bt.Insert(range[j]); } } for (int i = 1, cnt = 1; cnt <= constthread; cnt++) { int start = i; int end = (range.Length / constthread) * cnt; i = end; var t = new Task(() => { int[] rng = Enumerable.Range(start, end - start).ToArray(); BalancedRangeInsert(bt, rng, sw, true); }); t.Start(); wh.Add(t); } } else { var sw2 = new System.Diagnostics.Stopwatch(); for (int i = 2; i < range.Length; i *= 2) { for (int j = (range.Length / i); j < range.Length; j += (range.Length / i)) { int skip = 0; if (i > 2) { // Skip the previous round skip = (range.Length / (i / 2)); } if (j != skip) { sw2.Start(); bt.Insert(range[j]); sw2.Stop(); } } //Console.WriteLine("Time taken to balance insert {0} nodes: {1}ms", range.Length, sw2.Elapsed.TotalMilliseconds); } } }