Dear all,

I've timed the [parallel example from the 
docs](https://nim-lang.org/docs/manual_experimental.html#parallel-amp-spawn-parallel-statement)
 and found that it executes much slower than a single threaded version. With 
top -H I can see that threads are idle or underutilized. See below for the 
code. I've bumped the number of iterations to 1,000,000, set the number of 
threads to the number of processors on my machine (also tried with the default) 
and compiled with nim c -d:release and \--threads:on as appropriate.

Wall clock times:

  * no threading: 0:00.09
  * parallel: 0:21.85



Am I misusing this code or misunderstanding something?

Thanks, Andreas

**No threading:**
    
    
    import strutils, math
    
    proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
    
    proc pi(n: int): float =
      var ch = newSeq[float](n+1)
      for k in 0..ch.high:
        ch[k] = term(float(k))
      for k in 0..ch.high:
        result += ch[k]
    
    echo formatFloat(pi(1000000))

**Parallel version:**
    
    
    # Compute PI in an inefficient way
    import strutils, math
    import threadpool
    import cpuinfo
    {.experimental: "parallel".}
    
    let nProc = countProcessors()
    setMaxPoolSize(nProc)
    
    proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
    
    proc pi(n: int): float =
      var ch = newSeq[float](n+1)
      parallel:
        for k in 0..ch.high:
          ch[k] = spawn term(float(k))
      for k in 0..ch.high:
        result += ch[k]
    
    echo formatFloat(pi(1000000))

Reply via email to