Hacker News new | ask | show | jobs
by kingcub 5515 days ago
This to me is amazing in it's brevity:

  //Uses 4 threads
  (1 to 4).par.foreach(x => /*some long computation*/ }
I'd love to see how other languages can do it more tersely with their standard libraries.
2 comments

In python:

  from multiprocessing import Pool
  p = Pool(4) # 4 worker threads
  p.map(somefunc, someiter)
Not sure if this is apples-to-apples. Regardless, I'm always skeptical of the behavior of threaded code, even if it's embarrassingly parallel stuff that is divided among workers.
Unfortunately the multiprocessing module is a lot less efficient in many cases. Scala doesn't have to copy the entire collection across process boundaries or launch the runtime multiple times. (In this particular rather trivial example it doesn't matter much because an Int range is used). I'm afraid Python is losing out in the parallel world due to the GIL as more CPU cores become the norm.
In haskell you could write something like this: parMap rseq (\x->/some long computation/) [1..4]

using the standard Control.Parallel.Strategies library (use rwhnf instead of rseq unless you are using v3).