diff options
author | Araq <rumpf_a@web.de> | 2015-09-23 19:47:48 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2015-09-23 21:46:34 +0200 |
commit | 000c413f35d304576d77f3bebcf3193cc99cd411 (patch) | |
tree | 57bfac15465758d81365f9746551e024cddaca87 /tests | |
parent | f937637a926e8c9eba53d8de9ebdba932a344db4 (diff) | |
download | Nim-000c413f35d304576d77f3bebcf3193cc99cd411.tar.gz |
disjoint checker is smarter (and slower)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/parallel/tparfind.nim | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/parallel/tparfind.nim b/tests/parallel/tparfind.nim new file mode 100644 index 000000000..9de5012f5 --- /dev/null +++ b/tests/parallel/tparfind.nim @@ -0,0 +1,28 @@ +discard """ + output: "500" +""" + +import threadpool, sequtils + +{.experimental.} + +proc linearFind(a: openArray[int]; x, offset: int): int = + for i, y in a: + if y == x: return i+offset + result = -1 + +proc parFind(a: seq[int]; x: int): int = + var results: array[4, int] + parallel: + if a.len >= 4: + let chunk = a.len div 4 + results[0] = spawn linearFind(a[0 ..< chunk], x, 0) + results[1] = spawn linearFind(a[chunk ..< chunk*2], x, chunk) + results[2] = spawn linearFind(a[chunk*2 ..< chunk*3], x, chunk*2) + results[3] = spawn linearFind(a[chunk*3 ..< a.len], x, chunk*3) + result = max(results) + + +let data = toSeq(0..1000) +echo parFind(data, 500) + |