private static void _FastForEach(int threadIndex, IGetOrCreateValueSample sample) { for (int i = 0; i < _COUNT_ITERATIONS_FASTCREATOR; i++) { if (sample.GetOrCreate(i * 7) != -i * 7) { throw new Exception("An invalid result was got."); } } }
private static void _SlowSomeCollisionsForEach(int threadIndex, IGetOrCreateValueSample sample) { int multiplier = _COUNT_MANY_THREADS; for (long i = 0; i < _iterationsFor3Milliseconds; i++) { int value = (int)i * threadIndex; if (sample.GetOrCreate(value) != -value) { throw new Exception("An invalid result was got."); } } }
private static void _FastNonCollidingForEach(int threadIndex, IGetOrCreateValueSample sample) { int min = threadIndex * _COUNT_ITERATIONS_FASTCREATOR; int max = min + _COUNT_ITERATIONS_FASTCREATOR; for (int i = min; i < max; i++) { if (sample.GetOrCreate(i) != -i) { throw new Exception("An invalid result was got."); } } }
private static void _SlowSomeCollisionsForEach(int threadIndex, IGetOrCreateValueSample sample) { int multiplier = _COUNT_MANY_THREADS; for(long i=0; i<_iterationsFor3Milliseconds; i++) { int value = (int)i*threadIndex; if (sample.GetOrCreate(value) != -value) throw new Exception("An invalid result was got."); } }
private static void _FastNonCollidingForEach(int threadIndex, IGetOrCreateValueSample sample) { int min = threadIndex * _COUNT_ITERATIONS_FASTCREATOR; int max = min + _COUNT_ITERATIONS_FASTCREATOR; for(int i=min; i<max; i++) { if (sample.GetOrCreate(i) != -i) throw new Exception("An invalid result was got."); } }
private static void _FastForEach(int threadIndex, IGetOrCreateValueSample sample) { for(int i=0; i<_COUNT_ITERATIONS_FASTCREATOR; i++) if (sample.GetOrCreate(i*7) != -i*7) throw new Exception("An invalid result was got."); }
internal static void _Measure(int threadCount, ForEachDelegate forEachDelegate, IGetOrCreateValueSample sample) { Console.Write(sample.Message); using(var startCountdown = new CountdownEvent(threadCount)) { using(var canStartProcessingEvent = new ManualResetEvent(false)) { using(var finishCountdown = new CountdownEvent(threadCount)) { for(int threadIndex=0; threadIndex<threadCount; threadIndex++) { var thread = new Thread ( (p) => { int i = (int)p; startCountdown.Signal(); canStartProcessingEvent.WaitOne(); // tell that the thread is ready and wait for all threads to be ready // before starting. We should not start before all threads are created // and we should not consider the time to create the thread on our results. forEachDelegate(i, sample); finishCountdown.Signal(); } ); thread.Start(threadIndex); } // wait until all threads are created to start the stopwatch. startCountdown.Wait(); var stopwatch = new Stopwatch(); stopwatch.Start(); canStartProcessingEvent.Set(); // now we let all threads run. We should wait until all of them // finish to know the real result. finishCountdown.Wait(); stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed); } } } }
internal static void _Measure(int threadCount, ForEachDelegate forEachDelegate, IGetOrCreateValueSample sample) { Console.Write(sample.Message); using (var startCountdown = new CountdownEvent(threadCount)) { using (var canStartProcessingEvent = new ManualResetEvent(false)) { using (var finishCountdown = new CountdownEvent(threadCount)) { for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) { var thread = new Thread ( (p) => { int i = (int)p; startCountdown.Signal(); canStartProcessingEvent.WaitOne(); // tell that the thread is ready and wait for all threads to be ready // before starting. We should not start before all threads are created // and we should not consider the time to create the thread on our results. forEachDelegate(i, sample); finishCountdown.Signal(); } ); thread.Start(threadIndex); } // wait until all threads are created to start the stopwatch. startCountdown.Wait(); var stopwatch = new Stopwatch(); stopwatch.Start(); canStartProcessingEvent.Set(); // now we let all threads run. We should wait until all of them // finish to know the real result. finishCountdown.Wait(); stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed); } } } }