gpt4 book ai didi

scalaz-stream - 如何使用 Sink 提高代码性能?

转载 作者:行者123 更新时间:2023-12-04 14:44:19 25 4
gpt4 key购买 nike

我对 scalaz-streams 汇有奇怪的观察。他们工作缓慢。有谁知道这是为什么?有什么办法可以提高性能吗?

这是我的代码的相关部分:
无水槽版

//p is parameter with type p: Process[Task, Pixel]

def printToImage(img: BufferedImage)(pixel: Pixel): Unit = {
img.setRGB(pixel.x, pixel.y, 1, 1, Array(pixel.rgb), 0, 0)
}
val image = getBlankImage(2000, 4000)
val result = p.runLog.run
result.foreach(printToImage(image))

这需要 ~7s 来执行

带水槽的版本
//p is the same as before

def printToImage(img: BufferedImage)(pixel: Pixel): Unit = {
img.setRGB(pixel.x, pixel.y, 1, 1, Array(pixel.rgb), 0, 0)
}

//I've found that way of doing sink in some tutorial
def getImageSink(img: BufferedImage): Sink[Task, Pixel] = {
//I've tried here Task.delay and Task.now with the same results
def printToImageTask(img: BufferedImage)(pixel: Pixel): Task[Unit] = Task.delay {
printToImage(img)(pixel)
}
Process.constant(printToImageTask(img))
}



val image = getBlankImage(2000, 4000)
val result = p.to(getImageSink(image)).run.run

这个需要 33 秒来执行。由于这种显着差异,我在这里完全感到困惑。

最佳答案

在第二种情况下,您为每个像素分配 Task,而不是直接调用 printToImage,而是通过 Task 来完成它,并且调用链中的步骤要多得多。

我们经常使用 scalaz-stream,但我坚信将它用于此类问题是过度的。在 Process/Channel/Sink 中运行的代码应该比简单的变量分配/更新复杂得多。

我们使用接收器将数据从流写入数据库(Cassandra),我们使用批处理,写入单个行的开销很高。 Process/Sinks 是 super 方便的抽象,但适用于更高级的工作流。当编写 for 循环很容易时,我建议编写 for 循环。

关于scalaz-stream - 如何使用 Sink 提高代码性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26372343/

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