Should this be done in parallel?
No, because the GHC does not add parallelism by default (see below). In addition, parallelism is not a convenient orbital cannon that simply removes any problem (see below).
Why is my expectation wrong?
, runhaskell GHCi: , -O con & shy; flicts --interactive, RTS, , .
, ex & shy; e & shy; cu & shy; ta & shy; ble:
$ ghc --make -O2 -rtsopts -with-rtsopts -N -threaded SO.hs
$ .\SO +RTS -s
[31875000]
2,863,269,440 bytes allocated in the heap
1,135,584 bytes copied during GC
100,016 bytes maximum residency (2 sample(s))
31,152 bytes maximum slop
2 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 5471 colls, 5471 par 0.266s 0.283s 0.0001s 0.0126s
Gen 1 2 colls, 1 par 0.000s 0.001s 0.0004s 0.0007s
Parallel GC work balance: 0.00% (serial 0%, perfect 100%)
TASKS: 4 (1 bound, 3 peak workers (3 total), using -N2)
SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)
INIT time 0.000s ( 0.001s elapsed)
MUT time 20.328s ( 21.671s elapsed) <-------
GC time 0.266s ( 0.284s elapsed)
EXIT time 0.000s ( 0.000s elapsed)
Total time 20.609s ( 21.956s elapsed)
Alloc rate 140,852,608 bytes per MUT second
Productivity 98.7% of total user, 92.7% of total elapsed
, GHC parallelism . , parallelism , . , f :: Int -> T - , head $ filter p $ parallelMap f [1..100], , . head $ filter p $ parallelMap f [1..] . , Haskell .
parallelism
, , Haskell parallelism, , ? , :
triples :: [(Int, Int, Int)]
triples = filter pythagoras . filter smaller . filter sum1000 $ ts
where
pythagoras (a,b,c) = a ^ 2 + b ^ 2 == c ^ 2
sum1000 (a,b,c) = a + b + c == 1000
smaller (a,b,c) = a < b && b < c
ts = [(a,b,c) | a <- [0..1000], b <- [0..1000], c <- [0..1000]]
main :: IO ()
main = print $ map (\(a,b,c) -> a * b * c) $ triples
, . , . . sum1000 smaller . , smaller, , a b c, sum1000!
, a, b c:
a , 332, 1000 - 333 b c, smaller (667 = 333 + 334)b ab (1000 - a) / 2, cc 1000 - a - b, a = 0 b = 500 c.
:
triples :: [(Int, Int, Int)]
triples = filter pythagoras . filter smaller . filter sum1000 $ ts
where
pythagoras (a,b,c) = a ^ 2 + b ^ 2 == c ^ 2
sum1000 (a,b,c) = a + b + c == 1000
smaller (a,b,c) = a < b && b < c
ts = [(a,b,c) | a <- [0..332]
, b <- [a+1 .. (1000 - a)`div` 2]
, let c = 1000 - a - b]
-- Old list for documentation
-- ts = [(a,b,c) | a <- [0..1000], b <- [0..1000], c <- [0..1000]]
, b < c.
, O (n²) O (n³). runhaskell SO.hs 2 , , :
$ ghc --make -O2 SO.hs
$ ./SO +RTS -s
[31875000]
104,960 bytes allocated in the heap
1,752 bytes copied during GC
42,664 bytes maximum residency (1 sample(s))
18,776 bytes maximum slop
1 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 0 colls, 0 par 0.000s 0.000s 0.0000s 0.0000s
Gen 1 1 colls, 0 par 0.000s 0.000s 0.0005s 0.0005s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.000s ( 0.002s elapsed) <----------------
GC time 0.000s ( 0.000s elapsed)
EXIT time 0.000s ( 0.001s elapsed)
Total time 0.000s ( 0.003s elapsed)
%GC time 0.0% (13.9% elapsed)
Alloc rate 0 bytes per MUT second
Productivity 100.0% of total user, 0.0% of total elapsed
TL; DR
.