我正在尝试使用scala并行集合来实现一些cpu密集型任务,我想抽象出算法的执行方式(顺序,并行甚至分布式),但代码不能正常工作,因为我怀疑我不知道我做错了什么.
我想抽象出这个问题的方式如下:
// just measures time a block of code runs
def time(block: => Unit) : Long = {
val start = System.currentTimeMillis
block
val stop = System.currentTimeMillis
stop - start
}
// "lengthy" task
def work = {
Thread.sleep(100)
println("done")
1
}
import scala.collection.GenSeq
abstract class ContextTransform {
def apply[T](genSeq: GenSeq[T]): GenSeq[T]
}
object ParContextTransform extends ContextTransform {
override def apply[T](genSeq: GenSeq[T]): GenSeq[T] = genSeq.par
}
// this works as expected
def callingParDirectly = {
val range = (1 …Run Code Online (Sandbox Code Playgroud)