Last active
December 30, 2017 22:03
-
-
Save gfoidl/83e0bbda2d9e9e98bcce38d0b7e5091e to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApp1 | |
{ | |
static class Program | |
{ | |
static void Main() | |
{ | |
var ranges = new ConcurrentDictionary<int, List<(int Start, int End)>>(); | |
const int N = 10_000; | |
Parallel.ForEach( | |
new MyPartitioner(N), | |
range => | |
{ | |
AddRange(range); | |
for (int i = range.Start; i < range.End; ++i) | |
for (int j = 0; j < 1_000 * (N - range.Item2); ++j) | |
; | |
} | |
); | |
foreach (var item in ranges.OrderBy(i => i.Key)) | |
{ | |
Console.WriteLine($"Thread-ID: {item.Key}"); | |
foreach (var range in item.Value.OrderBy(r => r.Start)) | |
Console.WriteLine($"\t{range,-15}\t{range.End - range.Start}"); | |
} | |
//----------------------------------------------------------------- | |
void AddRange((int Start, int End) range) | |
{ | |
var list = new List<(int Start, int End)> { range }; | |
ranges.AddOrUpdate( | |
Thread.CurrentThread.ManagedThreadId, | |
list, | |
(id, l) => | |
{ | |
l.Add(range); | |
return l; | |
} | |
); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code for
MyPartitioner
is in https://gist.github.com/gfoidl/d8250ff11a5c70972cb8164622792ba1