We ported this to OF and it runs at about 5 fps in Debug, and 40 fps in Release, which I find really surprising. And adding any more blobs or increasing the image size basically just breaks down.
Running your code in Release gives me about 80 fps. Just by making some function calls outside of the loop, i get around 155fps.
Here is the code:
auto pixels = canvas.getPixels().getData();
auto width = ofGetWidth();
auto height = ofGetHeight();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int index = x + y * width;
glm::vec2 vec(x, y);
float sum = 0;
for (Blob& b : blobs)
{
sum += 10 * b.r / glm::distance(vec, b.pos);
}
pixels[index] = min(255.0f, sum);
}
}
canvas.update();
Also, you will get A LOT more fps if you do this in a shader.
I have found calls to ofGetWidth() ofGetHeight() and ofGetElapsedTimef() to be a little slow, which can be painful inside of for loops – perhaps you can look there first? Looks like that’s something marco91i optimized.
also on osx, you can use the time profiler in Xcode to see what your code is spending time on – it’s usually how I can suss out slow function calls.
@prisonerjohn Im doing teaching with openFrameworks as well. I sometimes do a pretty similar exercise and after that im letting them do the same with a shader, just to see the performance increase.
I got curious with your example and just wrote a shader that does the same - and i get around 3500fps . In case youre interested, heres the code:
i would also swap the x and y for loops so you iterate pixels one after another as they are laid out in memory. as it is you are jumping in memory which can decrease speed a lot depending on what you are doing inside the loop