对于不大的n
,串行代码总是会更快(在尾递归的情况下要快得多)。这是因为调用一个新的函数会比启动一个新的actor更快。另外,线程和上下文切换之间会存在争用。
在下面的代码中,我为每个n > 2
. 可以有很多优化的方法,但我只是简单地使用递归T(n) = T(n-1) + T(n-2)
到串行一个。
import akka.actor.Actor
import akka.actor.Props
import akka.actor.ActorSystem
import akka.event.Logging
import akka.actor.ActorRef
import akka.routing.RoundRobinRouter
object Fib extends App {
trait Fib
case class N(val n: Int) extends Fib
case class Ans(n: Int)
class FibN(listen: ActorRef) extends Actor {
var numOfResults = 0;
var ans = 0;
val log = Logging(context.system, this)
def receive = {
case N(x) => {
//println(self.path+"-Message N(x) "+x)
val others = context.actorOf(Props(new FibN(self)).withRouter(RoundRobinRouter(2)), name = "Fibn:" + x)
if(x==1 || x==2)
listen ! new Ans(1)
else if(x>2){
others ! new N(x-1)
others ! new N(x-2)
}
}
case Ans(x) => {
//println(self.path+" Ans(x) "+x+" numOfResults "+numOfResults+" from "+sender.path)
numOfResults += 1
ans = ans + x;
if (numOfResults == 2){
//println(self.path+"sending back to sender "+listen.path+" numOfResults "+numOfResults)
listen ! Ans(ans)
}
}
case _ => println(self.path+"Not valid")
}
}
class Listener extends Actor{
val log = Logging(context.system, this)
var st:Long = 0;
def receive = {
case Ans(x) => {
println(self.path+"\n\nAns is "+x+" time taken: "+(System.currentTimeMillis() - st))
context.system.shutdown
}
case N(x) => {
println(self.path+" Message Received "+x)
val actor = context.actorOf(Props(new FibN(self)),"FibN")
st = System.currentTimeMillis()
actor ! new N(x)
}
case _ => println(self.path+" Invalid request")
}
}
val system = ActorSystem("Fibanoccia")
val listener = system.actorOf(Props[Listener],"Listener")
listener ! new N(25)
}
正如预期的那样,这要慢得多。除非n
非常大,否则由于上述原因,actor 总是会变慢。对于较大的“n”,可以分解。