static string BenchmarkClassicWrite(int acountCount, int range, int runs, int warmUpRuns) { var rand = new Random(); var actions = new List <Action>(); Action stepUp = () => { var tree = new ConcurrentDictionary <int, int>(); actions = new List <Action>(); for (int i = 0; i < acountCount; i++) { var number = rand.Next(range); actions.Add(() => { tree.GetOrAdd(number, number); }); } }; Action run = () => { Parallel.Invoke(actions.ToArray()); }; return(MyStupidBenchmarker.Benchmark(stepUp, run, runs, warmUpRuns).ToString()); }
static string BenchmarkClassicRead(int acountCount, int runs, int warmUpRuns) { var rand = new Random(); var actions = new List <Action>(); Action stepUp = () => { var tree = new ConcurrentDictionary <Guid, string>(); actions = new List <Action>(); for (int i = 0; i < acountCount; i++) { var guid = Guid.NewGuid(); tree.GetOrAdd(guid, guid.ToString()); actions.Add(() => { tree.TryGetValue(guid, out var _); }); } }; Action run = () => { Parallel.Invoke(actions.ToArray()); }; return(MyStupidBenchmarker.Benchmark(stepUp, run, runs, warmUpRuns).ToString()); }
static string BenchmarkMine(int acountCount, int range, int runs, int warmUpRuns) { var rand = new Random(); List <Action> actions = null;; Action stepUp = () => { var tree = new RawConcurrentIndexed <int, int>(); actions = new List <Action>(); for (int i = 0; i < acountCount; i++) { var roll = rand.Next(2); if (roll == 0) { var number = rand.Next(range); actions.Add(() => { tree.GetOrAdd(number, number); }); } else if (roll == 1) { var number = rand.Next(range); actions.Add(() => { tree.TryGetValue(number, out var _); }); } } }; Action run = () => { Parallel.Invoke(actions.ToArray()); }; return(MyStupidBenchmarker.Benchmark(stepUp, run, runs, warmUpRuns).ToString()); }
static string JustReadFromAnArrayALot(int acountCount, int runs, int warmUpRuns) { var rand = new Random(); List <Action> actions = null; ArrayHolder ah = new ArrayHolder(); Action stepUp = () => { var tree = new string[100]; for (int i = 0; i < 100; i++) { tree[i] = "thing " + i; } ah.array = tree; Action action = () => { var _ = ah.array.Span[(int)rand.Next(100)]; }; actions = new List <Action>(); for (int i = 0; i < acountCount; i++) { actions.Add(action); } }; Action run = () => { Parallel.Invoke(actions.ToArray()); }; return(MyStupidBenchmarker.Benchmark(stepUp, run, runs, warmUpRuns).ToString()); }