What I understand, Haskell have green threads. But how light weight are they. Is it possible to create 1 million threads?
Or How long would it take for 100 000 threads?
For this synthetic test case, spawning hardware threads results in significant overheads. Working just with green threads looks like a preferred option. Note that spawning green threads in Haskell is indeed cheap. I've re-run the above program, with n = 1m on MacBook Pro, i7, 8GB of RAM, using:
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.6.3
Compiled with -threaded and -rtsopts:
$ time ./thr
1000000
real 0m5.974s
user 0m3.748s
sys 0m2.406s
Reducing the stack helps a bit:
$ time ./thr +RTS -k0.5k
1000000
real 0m4.804s
user 0m3.090s
sys 0m1.923s
Then, compiled without -threaded:
$ time ./thr
1000000
real 0m2.861s
user 0m2.283s
sys 0m0.572s
And finally, without -threaded and with reduced stack:
$ time ./thr +RTS -k0.5k
1000000
real 0m2.606s
user 0m2.198s
sys 0m0.404s