Am I doing this correctly?
It is a trivial test, but threading is W..A..Y slower.
Also, how do you get doTestThreaded2() to successfully compile?
(I am doing this on Windows)
import sequtils, threadpool, times, strutils var # loop to make it slow enough to time the calculations loopCnt: int = 100000 n: int = 4 # <= number of spawns res: int = 0 #r: seq[int] r {.threadvar.}: seq[int] t0 = cpuTime() proc M(i,j:int): int {.thread.} = result = i*j proc doTestThreaded1(): int = result = 0 for i in 0.. <n: for j in 0.. <n: r[j] = ^(spawn(M(i,j))) sync() for j in 0.. <n: result += r[j] proc doTestThreaded2(): int = result = 0 for i in 0.. <n: # THIS DOESN'T WORK (prove j <= len(r) + -1) #for j in 0.. <n: # parallel: # r[j] = spawn(M(i,j)) for j in 0.. <n: result += r[j] proc doTestUnthreaded: int = result = 0 for i in 0.. <n: for j in 0.. <n: result += M(i,j) ########### r = newSeq[int](n) t0 = cpuTime() for cnt in 1..loopCnt: res += doTestUnthreaded() echo "Result: $1 took $2 seconds" % [$res,$(cpuTime()-t0)] res = 0 t0 = cpuTime() for cnt in 1..loopCnt: res += doTestThreaded1() echo "Result: $1 took $2 seconds" % [$res,$(cpuTime()-t0)]
proc doTestThreaded2(): int =
result = 0
var r = newSeq(n)
for i in 0 .. high(r):
parallel:
for j in 0.. high(r):
r[j] = spawn(M(i,j))
for j in 0.. r.high:
result += r[j]