26 lines
629 B
Plaintext
26 lines
629 B
Plaintext
# approximate pi by integrating 1/(1+x²) from 0 to 1,
|
||
# doing so in parallel with threading for a speed-up.
|
||
|
||
f ← ˜÷1+1ⁿ2
|
||
# create interval [a, b) with n values
|
||
# interval ? a b n
|
||
ι ← ⍜-×⊙⊙(÷⟜⇡)
|
||
# integrate f on given interval
|
||
# integral ? f a b n
|
||
i! ← ÷⊃⧻/+ ^0 ι
|
||
# time a unary function
|
||
# ? f
|
||
t! ← ⊙(&p" ms"&pf×1e3⌵-)⊙now^0⊙now
|
||
# partition into n subintervals
|
||
# [subintervals a_b] ? a b n
|
||
p ← ⊂⟜(\+↯⤙(˜÷⌵-))
|
||
|
||
p 0 1 4 # n=4 threads
|
||
⊃(&p $ sequential:
|
||
t!(⧈₂/(i!f⊙⊙1e7))
|
||
| &p $ parallel:
|
||
t!(wait⧈₂/pool(i!f⊙⊙1e7))
|
||
)
|
||
&p $ error:
|
||
&p ⌵-π×4÷⊃⧻/+ ◌
|