diff --git a/README.md b/README.md index 7833f2f..ba86faa 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,10 @@ Number of words|Time to check all anagrams no longer than that|Time to solve "ea ---------------|----------------------------------------------|-------------------------|-----------------------------------|-------------------------|--------------------------------------------- 3|0.04s||||4560 4|0.65s|||0.1s|7,431,984 -5|21s|0.3s|0.1s|0.6s|1,347,437,484 -6|12.5 minutes|2.2s|0.4s|5.4s|58,405,904,844 -7|5 hours|12.5s|1.6s|35s|1,070,307,744,114 -8|49 hours|46s|4.7s|2.5 minutes|10,893,594,396,594 +5|19s|0.3s|0.1s|0.5s|1,347,437,484 +6|10.5 minutes|1.9s|0.35s|4.7s|58,405,904,844 +7|3.2 hours|11s|1.4s|31.5s|1,070,307,744,114 +8|49 hours|40s|4.1s|2.1 minutes|10,893,594,396,594 9||2.5 minutes|13s|9.5 minutes|70,596,864,409,954 10||5 minutes|21s|17.5 minutes|314,972,701,475,754 @@ -61,8 +61,6 @@ For comparison, certain other solutions available on GitHub seem to require 3 ho Conditional compilation symbols =============================== -* Define `SINGLE_THREADED` to use standard enumerables instead of ParallelEnumerable (useful for profiling). - * Define `DEBUG`, or build in debug mode, to get the total number of anagrams (not optimized). Implementation notes diff --git a/dotnet/WhiteRabbit/Constants.cs b/dotnet/WhiteRabbit/Constants.cs index 016338e..3fa1847 100644 --- a/dotnet/WhiteRabbit/Constants.cs +++ b/dotnet/WhiteRabbit/Constants.cs @@ -5,5 +5,7 @@ public const int PhrasesPerSet = WhiteRabbitUnmanagedBridge.MD5Unmanaged.PhrasesPerSet; public const int MaxNumberOfWords = 8; + + public const int NumberOfThreads = 4; } } diff --git a/dotnet/WhiteRabbit/StringsProcessor.cs b/dotnet/WhiteRabbit/StringsProcessor.cs index 1159171..69777f5 100644 --- a/dotnet/WhiteRabbit/StringsProcessor.cs +++ b/dotnet/WhiteRabbit/StringsProcessor.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; + using System.Threading.Tasks; internal sealed class StringsProcessor { @@ -65,14 +66,18 @@ var sums = this.VectorsProcessor.GenerateSequences(); // converting sequences of vectors to the sequences of words... - var result = from sum in sums.AsParallel() - let filter = ComputeFilter(sum) - let wordsVariants = this.ConvertVectorsToWordIndexes(sum) - from wordsArray in Flattener.Flatten(wordsVariants) - from phraseSet in this.ConvertWordsToPhrases(wordsArray, filter) - select phraseSet; - - result.ForAll(action); + Parallel.ForEach(sums, new ParallelOptions { MaxDegreeOfParallelism = Constants.NumberOfThreads }, sum => + { + var filter = ComputeFilter(sum); + var wordsVariants = this.ConvertVectorsToWordIndexes(sum); + foreach (var wordsArray in Flattener.Flatten(wordsVariants)) + { + foreach (var phraseSet in this.ConvertWordsToPhrases(wordsArray, filter)) + { + action(phraseSet); + } + } + }); } public long GetPhrasesCount()