This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class QueuingAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) | |
| { | |
| var queue = new Queue<int>(values); | |
| var sum = 0; | |
| while (queue.TryDequeue(out int value)) | |
| { | |
| sum += value; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class ParallelAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) | |
| { | |
| var queue = new ConcurrentQueue<int>(values); | |
| int overallSum = 0; | |
| Action addFromQueue = () => | |
| { | |
| int localSum = 0; | |
| while (queue.TryDequeue(out int localValue)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class ConcurrentBatchAdder : IIntegerAdder | |
| { | |
| private const int degreeOfParallelism = 4; | |
| public int Add(int[] values) | |
| { | |
| var countPerSegment = values.Length / degreeOfParallelism + 1; | |
| int[,] segments = SplitIntoSegments(values, countPerSegment); | |
| var sums = new int[degreeOfParallelism]; |
OlderNewer