I have weird observation about scalaz-streams sinks. They are working slow. Does anyone know why is that? And is there any way to improve the performance?
here are relevant parts of my code: version without sink
//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))
this takes ~7s to execute
version with sink
//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
this one takes 33 seconds to execute. I am totally confused here because of that significant difference.
10 Tips for C and C++ Performance Improvement Code Optimization. 1. Optimize your Code using Appropriate Algorithm. For any code you write, you should always take some time to think through and pick the right ... 2. Optimize Your Code for Memory. 3. printf and scanf Vs cout and cin. 4. Using ...
How to speed up the code in 5 easy steps? If you are writing a command that has to a large amount of data, consider the following steps, that will ensure good performance and optimized memory usage: 1. Plan your code Make sure your code is well planned. Some things you can do before others, to avoid repeating the same actions.
In such cases, we are providing some tips which a developer can keep in mind while doing coding of any requirement so that he/she needs to make minimal changes to code for fixing the performance during the testing phase or before moving the same to production. 1. Avoid Writing Long Methods
Optimize your Code using Appropriate Algorithm For any code you write, you should always take some time to think through and pick the right algorithm to use for your specific scenario. The problem we are going to analyze for this example is to find a maximum value of the function in a two dimensional segment. We’ll consider only whole numbers.
In second case you are allocating Task for each pixel, and instead of directly calling printToImage you do it through Task, and it's much more steps in a call-chain.
We use scalaz-stream a lot, but I strongly believe that it's overkill to use it for this type problems. Code running inside Process/Channel/Sink should much more complicated than simple variable assignment/update.
We use Sinks to write data from stream into databases (Cassandra) and we use batching, it's to high overhead to write individual rows. Process/Sinks is super convenient abstraction, but for more high level workflows. When it's easy to write for-loop I would suggest to write for-loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With