versioninfo()
Julia Version 1.3.0 Commit 46ce4d7933 (2019-11-26 06:09 UTC) Platform Info: OS: Linux (x86_64-pc-linux-gnu) CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-6.0.1 (ORCJIT, skylake) Environment: JULIA_NUM_THREADS = 4
Threads.nthreads()
4
function fib_naive(n)
if n ≤ 1
n
else
fib_naive(n - 2) + fib_naive(n - 1)
end
end
fib_naive (generic function with 1 method)
fib_naive(10)
55
@time fib_naive(40)
0.726073 seconds (5 allocations: 176 bytes)
102334155
fib_request(n; spawn=true) = Channel{Int}(spawn=spawn) do channel
put!(channel, fib_naive(n))
end
fib_request (generic function with 1 method)
future = fib_request(40)
Channel{Int64}(sz_max:0,sz_curr:0)
take!(future)
102334155
function main(nrequests, nfib=40)
requests = [fib_request(nfib) for _=1:nrequests]
[take!(req) for req in requests]
end
main (generic function with 2 methods)
main(4)
4-element Array{Int64,1}: 102334155 102334155 102334155 102334155
@time main(4)
0.784660 seconds (148 allocations: 7.969 KiB)
4-element Array{Int64,1}: 102334155 102334155 102334155 102334155
function main2(nrequests, nfib=40)
requests = [fib_request(nfib, spawn=false) for _=1:nrequests]
[take!(req) for req in requests]
end
main2 (generic function with 2 methods)
main2(4)
4-element Array{Int64,1}: 102334155 102334155 102334155 102334155
@time main2(4)
2.925072 seconds (140 allocations: 7.813 KiB)
4-element Array{Int64,1}: 102334155 102334155 102334155 102334155