public static void ParallelLinqFuzzyMatch() { BenchPerformance.Time("Parallel Linq Fuzzy Match", iterations: Data.Iterations, operation: () => { ParallelQuery <string> matches = (from word in WordsToSearch.AsParallel() from match in FuzzyMatch.JaroWinklerModule.bestMatch(Words, word) select match.Word); matches.ForAll(match => { Log(match); }); }); }
public static void ParallelLinqPartitionerFuzzyMatch() { BenchPerformance.Time("Parallel PLinq partitioner Fuzzy Match", iterations: Data.Iterations, operation: () => { var partitioner = Partitioner.Create(WordsToSearch, EnumerablePartitionerOptions.NoBuffering); ParallelQuery <string> matches = (from word in WordsToSearch.AsParallel() from match in FuzzyMatch.JaroWinklerModule.bestMatch(Words, word) select match.Word); matches.ForAll(match => { Log(match); }); }); }
public void ParallelLinqFuzzyMatch() { BenchPerformance.Time("Parallel Linq Fuzzy Match", iterations: Data.Iterations, operation: () => { ParallelQuery <string> matches = (from word in WordsToSearch.AsParallel() from match in FuzzyMatch.JaroWinklerModule.bestMatch(Words, word) select match.Word); foreach (var match in matches) { Console.Write("{0}\t", match); } Console.WriteLine(); }); }
public void TwoThreadsFuzzyMatch() { List <string> matches = new List <string>(); BenchPerformance.Time("Two Thread Fuzzy Match", iterations: Data.Iterations, operation: () => { var t1 = new Thread(() => { var take = WordsToSearch.Count / 2; var start = 0; foreach (var word in WordsToSearch.Take(take)) { var localMathes = FuzzyMatch.JaroWinklerModule.bestMatch(Words, word); matches.AddRange(localMathes.Select(m => m.Word)); } }); var t2 = new Thread(() => { var start = WordsToSearch.Count / 2; var take = WordsToSearch.Count - start; foreach (var word in WordsToSearch.Skip(start).Take(take)) { var localMathes = FuzzyMatch.JaroWinklerModule.bestMatch(Words, word); matches.AddRange(localMathes.Select(m => m.Word)); } }); t1.Start(); t2.Start(); t1.Join(); t2.Join(); }); foreach (var match in matches.Distinct()) { Console.Write("{0}\t", match); } Console.WriteLine(); }
public void MultipleThreadsFuzzyMatch() { List <string> matches = new List <string>(); BenchPerformance.Time("Multi Thread Fuzzy Match", iterations: Data.Iterations, operation: () => { var threads = new Thread[Environment.ProcessorCount]; for (int i = 0; i < threads.Length; i++) { var index = i; threads[index] = new Thread(() => { var take = WordsToSearch.Count / (Math.Min(WordsToSearch.Count, threads.Length)); var start = index == threads.Length - 1 ? WordsToSearch.Count - take : index * take; foreach (var word in WordsToSearch.Skip(start).Take(take)) { var localMathes = FuzzyMatch.JaroWinklerModule.bestMatch(Words, word); matches.AddRange(localMathes.Select(m => m.Word)); } }); } for (int i = 0; i < threads.Length; i++) { threads[i].Start(); } for (int i = 0; i < threads.Length; i++) { threads[i].Join(); } }); foreach (var match in matches.Distinct()) { Console.Write("{0}\t", match); } Console.WriteLine(); }
public async Task ProcessAsynchronously() { const int MDP = 1; var splitLines = new TransformBlock <Tuple <string, string>, Tuple <string, string[]> >( n => { string nameText = n.Item1; string text = n.Item2; string[] lines = text.Split('\n'); #if CONSOLEAGENT consoleAgent.Post(Tuple.Create(1, string.Format("Text {0} received - Splitting Lines on thread id {1}", nameText, Thread.CurrentThread.ManagedThreadId))); #else ConsoleColor backupColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Text {0} received - Splitting Lines on thread id {1}", nameText, Thread.CurrentThread.ManagedThreadId); Console.ForegroundColor = backupColor; #endif Thread.Sleep(TimeSpan.FromMilliseconds(100)); return(Tuple.Create(nameText, lines)); }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = MDP, CancellationToken = cts.Token }); var splitWords = new TransformBlock <Tuple <string, string[]>, Tuple <string, string[]> >( n => { string nameText = n.Item1; string[] lines = n.Item2; #if CONSOLEAGENT consoleAgent.Post(Tuple.Create(2, string.Format("Text {0} received - Splitting Words on thread id {1}", nameText, Thread.CurrentThread.ManagedThreadId))); #else ConsoleColor backupColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Text {0} received - Splitting Words on thread id {1}", nameText, Thread.CurrentThread.ManagedThreadId); Console.ForegroundColor = backupColor; #endif string[] words = (from line in lines from word in line.Split(Delimiters) select word.ToUpper()).ToArray(); Thread.Sleep(TimeSpan.FromMilliseconds(100)); return(Tuple.Create(nameText, words)); }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = MDP, CancellationToken = cts.Token }); var fuzzyMatch = new TransformBlock <Tuple <string, string[]>, Tuple <string, string[]> >( n => { string nameText = n.Item1; string[] words = n.Item2; #if CONSOLEAGENT consoleAgent.Post(Tuple.Create(3, string.Format("Text {0} received - Fuzzy Match on thread id {1}", nameText, Thread.CurrentThread.ManagedThreadId))); #else ConsoleColor backupColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Text {0} received - Fuzzy Match on thread id {1}", nameText, Thread.CurrentThread.ManagedThreadId); Console.ForegroundColor = backupColor; #endif var matches = (from wordToSearch in WordsToSearch.AsParallel() from match in FuzzyMatch.JaroWinklerModule.Parallel.bestMatch(words, wordToSearch) select match.Word).ToArray(); Thread.Sleep(TimeSpan.FromMilliseconds(100)); return(Tuple.Create(nameText, matches)); } , new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = MDP, CancellationToken = cts.Token }); var sendBackResult = new ActionBlock <Tuple <string, string[]> >( s => { string nameText = s.Item1; string[] matches = s.Item2; #if CONSOLEAGENT consoleAgent.Post(Tuple.Create(4, string.Format("The final result sending back on thread id {1}", Thread.CurrentThread.ManagedThreadId))); #else ConsoleColor backupColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("The final result sending back on thread id {1}", s, Thread.CurrentThread.ManagedThreadId); Console.ForegroundColor = backupColor; #endif agent.Post(Tuple.Create(nameText, matches)); } , new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = MDP, CancellationToken = cts.Token }); try { InputBlock.LinkTo(splitLines, new DataflowLinkOptions { PropagateCompletion = true }); splitLines.LinkTo(splitWords, new DataflowLinkOptions { PropagateCompletion = true }); splitWords.LinkTo(fuzzyMatch, new DataflowLinkOptions { PropagateCompletion = true }); fuzzyMatch.LinkTo(sendBackResult, new DataflowLinkOptions { PropagateCompletion = true }); await sendBackResult.Completion; consoleAgent.Post(Tuple.Create(999, string.Format("{0}\nPress ENTER to exit.\n{0}", new string('*', 30)))); } catch (OperationCanceledException) { Console.WriteLine("Operation has been canceled! Press ENTER to exit."); } Console.ReadLine(); }