gpt4 book ai didi

scala - 与Frege一起运行的Akka比Scala竞争对手慢

转载 作者:行者123 更新时间:2023-12-04 04:17:46 24 4
gpt4 key购买 nike

作为练习,我将这些Akka的ScalaJava示例移植到Frege。虽然运行良好,但运行速度比Scala(540ms)慢了11秒。

module mmhelloworld.akkatutorialfregecore.Pi where
import mmhelloworld.akkatutorialfregecore.Akka

data PiMessage = Calculate |
Work {start :: Int, nrOfElements :: Int} |
Result {value :: Double} |
PiApproximation {pi :: Double, duration :: Duration}

data Worker = private Worker where
calculatePiFor :: Int -> Int -> Double
calculatePiFor !start !nrOfElements = loop start nrOfElements 0.0 f where
loop !curr !n !acc f = if n == 0 then acc
else loop (curr + 1) (n - 1) (f acc curr) f
f !acc !i = acc + (4.0 * fromInt (1 - (i `mod` 2) * 2) / fromInt (2 * i + 1))

onReceive :: Mutable s UntypedActor -> PiMessage -> ST s ()
onReceive actor Work{start=start, nrOfElements=nrOfElements} = do
sender <- actor.sender
self <- actor.getSelf
sender.tellSender (Result $ calculatePiFor start nrOfElements) self

data Master = private Master {
nrOfWorkers :: Int,
nrOfMessages :: Int,
nrOfElements :: Int,
listener :: MutableIO ActorRef,
pi :: Double,
nrOfResults :: Int,
workerRouter :: MutableIO ActorRef,
start :: Long } where

initMaster :: Int -> Int -> Int -> MutableIO ActorRef -> MutableIO UntypedActor -> IO Master
initMaster nrOfWorkers nrOfMessages nrOfElements listener actor = do
props <- Props.forUntypedActor Worker.onReceive
router <- RoundRobinRouter.new nrOfWorkers
context <- actor.getContext
workerRouter <- props.withRouter router >>= (\p -> context.actorOf p "workerRouter")
now <- currentTimeMillis ()
return $ Master nrOfWorkers nrOfMessages nrOfElements listener 0.0 0 workerRouter now

onReceive :: MutableIO UntypedActor -> Master -> PiMessage -> IO Master
onReceive actor master Calculate = do
self <- actor.getSelf
let tellWorker start = master.workerRouter.tellSender (work start) self
work start = Work (start * master.nrOfElements) master.nrOfElements
forM_ [0 .. master.nrOfMessages - 1] tellWorker
return master
onReceive actor master (Result newPi) = do
let (!newNrOfResults, !pi) = (master.nrOfResults + 1, master.pi + newPi)
when (newNrOfResults == master.nrOfMessages) $ do
self <- actor.getSelf
now <- currentTimeMillis ()
duration <- Duration.create (now - master.start) TimeUnit.milliseconds
master.listener.tellSender (PiApproximation pi duration) self
actor.getContext >>= (\context -> context.stop self)
return master.{pi=pi, nrOfResults=newNrOfResults}

data Listener = private Listener where
onReceive :: MutableIO UntypedActor -> PiMessage -> IO ()
onReceive actor (PiApproximation pi duration) = do
println $ "Pi approximation: " ++ show pi
println $ "Calculation time: " ++ duration.toString
actor.getContext >>= ActorContext.system >>= ActorSystem.shutdown

calculate nrOfWorkers nrOfElements nrOfMessages = do
system <- ActorSystem.create "PiSystem"
listener <- Props.forUntypedActor Listener.onReceive >>= flip system.actorOf "listener"
let constructor = Master.initMaster nrOfWorkers nrOfMessages nrOfElements listener
newMaster = StatefulUntypedActor.new constructor Master.onReceive
factory <- UntypedActorFactory.new newMaster
masterActor <- Props.fromUntypedFactory factory >>= flip system.actorOf "master"
masterActor.tell Calculate
getLine >> return () --Not to exit until done

main _ = calculate 4 10000 10000

我对Akka的处理方式有问题吗,还是因为Frege的缓慢而造成的懒惰?例如,当我最初用 fold(严格折叠)代替 loop中的 Worker.calculatePiFor时,花费了27秒。

依存关系:
  • Frege的Akka原生定义:Akka.fr
  • Java助手来扩展Akka类,因为我们不能在
    弗雷格:Actors.java
  • 最佳答案

    我对Actors并不十分熟悉,但是假设最紧密的循环确实是loop,则可以避免将f函数作为参数传递。

    首先,传递函数的应用程序无法利用实际传递函数的严格性。而是,代码生成必须保守地假设所传递的函数延迟地接受其参数并返回延迟的结果。

    其次,在本例中,您实际上只在这里使用了f一次,因此可以内联它。 (这是您链接的文章的scala代码中完成此操作的方法。)

    在下面的模拟您的示例代码中查看为尾部递归生成的代码:

    test b c = loop 100 0 f 
    where
    loop 0 !acc f = acc
    loop n !acc f = loop (n-1) (acc + f (acc-1) (acc+1)) f -- tail recursion
    f x y = 2*x + 7*y

    我们到达那里:
    // arg2$f is the accumulator
    arg$2 = arg$2f + (int)frege.runtime.Delayed.<java.lang.Integer>forced(
    f_3237.apply(PreludeBase.INum_Int._minusƒ.apply(arg$2f, 1)).apply(
    PreludeBase.INum_Int._plusƒ.apply(arg$2f, 1)
    ).result()
    );

    您会在此处看到 f延迟调用,这也会导致所有参数expressios也被延迟计算。请注意这要求的方法调用数量!
    在您的情况下,代码仍应类似于:
    (double)Delayed.<Double>forced(f.apply(acc).apply(curr).result())

    这意味着,将使用盒装值acc和curr构建两个闭包,然后计算结果,即,使用未盒装参数调用函数 f,然后再次将盒装结果,只是为了在下一个步骤再次将盒装(强制)环形。

    现在比较以下内容,我们只是不传递 f而是直接调用它:
    test b c = loop 100 0 
    where
    loop 0 !acc = acc
    loop n !acc = loop (n-1) (acc + f (acc-1) (acc+1))
    f x y = 2*x + 7*y

    我们得到:
    arg$2 = arg$2f + f(arg$2f - 1, arg$2f + 1);

    好多了!
    最后,在上述情况下,我们完全可以不执行任何函数调用:
          loop n !acc = loop (n-1) (acc + f) where
    f = 2*x + 7*y
    x = acc-1
    y = acc+1

    得到的是:
    final int y_3236 = arg$2f + 1;
    final int x_3235 = arg$2f - 1;
    ...
    arg$2 = arg$2f + ((2 * x_3235) + (7 * y_3236));

    请尝试一下,让我们知道会发生什么。性能的主要提高应该来自不传递 f,而内联无论如何都可能会在JIT中完成。
    fold的额外费用可能是因为您还必须在应用列表之前创建一些列表。

    关于scala - 与Frege一起运行的Akka比Scala竞争对手慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17256979/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com