本文介绍了如何并行运行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何并行地同时运行这两个独立的循环.
How can i run these two independent loops simultaneously in parallel.
let a1=Seq.map2 (fun a b->(1.0-a)/(a-b)) a2 a3
let b1=Seq.map2 (fun a b->a*b) b2 b3
推荐答案
您可以为此使用标准.NET任务-没有特殊的F#函数或特殊的语法可在后台生成计算:
You can use standard .NET tasks for this - there are no special F# functions or special syntax for spawning a computation in the background:
let a1Work = Task.Factory.StartNew(fun () ->
Array.map2 (fun a b->(1.0-a)/(a-b)) a2 a3)
let b1 = Array.map2 (fun a b->a*b) b2 b3
let a1 = a1Work.Value
我也将您的Seq.map2
更改为Array.map2
-序列的计算是惰性的,因此在任务中运行它们实际上不会执行任何操作.使用数组,整个计算将立即完成.
I also changed your Seq.map2
to Array.map2
- computations on sequences are lazy and so running them inside a task would not actually do anything. With arrays, the whole calculation is completed immediately.
这篇关于如何并行运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!