function foo(n)
a = Array{Int64}(undef, n)
for i in 1:n
a[i] = i
end
return a
end
@sync
和 @async
组合来避免同步操作,从而提高并行代码的性能。function do_parallel_work()
@sync begin
@async begin
print("Doing some work...")
end
end
end
using Distributed
addprocs(4) # 添加 4 个进程
function parallel_work()
# 执行一些并行任务
end
@everywhere function worker()
parallel_work()
end
pmap(worker, 1:100) # 调整并行工作负载
上一篇:并行计算字符总和