[Closed] Blend-Mode Code Optimization
I’m looking to optimize some overnight rendering/comp scripts which are sometimes not quick enough to finish a queue (48 or so) of images (1575×1250 & 2400×1575) before 8am rolls around. What I’m wondering is which, if either, of the two bits of code should technically be more efficient.
Some background info:
- ambientPass, occlusionPass and comp1 are bitmap values with identical dimensions.
- overlayBlend is a function which composites 2 pixels and returns a pixel color value.
Code sample 1:
for j=0 to ambientPass.height-1 do
(
for i=0 to ambientPass.width-1 do
(
ambientPixel = getPixels ambientPass [i,j] 1
occlusionPixel = getPixels occlusionPass [i,j] 1
outputPixel = overlayBlend ambientPixel[1] occlusionPixel[1]
outputPixel = #(outputPixel)
setPixels comp1 [i,j] outputPixel
)
)
Code sample 2:
for j=0 to ambientPass.height-1 do
(
ambientPixels = getPixels ambientPass [0,j] ambientPass.width
occlusionPixels = getPixels occlusionPass [0,j] ambientPass.width
outputPixel = #()
for i=1 to ambientPass.width do
(
append outputPixel (overlayBlend ambientPixels[i] occlusionPixels[i])
setPixels comp1 [0,j] outputPixel
)
)
As you can see, the difference between the two is that in sample 1, the pixels are collected one at a time, while in sample 2 an entire row of pixels is collected at a time. Am I correct in assuming sample 2 would be the most efficient? I realize that collecting outside of the nested loop is much quicker, as I’m collecting only once per row of pixels, but I just wanted some verification that I’m not overlooking something that would negate this. Also, considering that this block of code, in one incarnation or another, is executed 3 times for each image, how significant would the difference in render time be on a set of 48 images?
Ok, after a few tests, it seems that code sample 1 is actually faster than sample 2, but I have no idea why…sample 1 produced a comp’d image in approximately 13 minutes. Sample 2 took nearly 22 minutes. The only thing I can think that would be the cause of this is if, in an increasingly large array, appending values to that array take longer for each successive append operation; maybe if each member of the array had to be passed over before appending. This would make sense, seing as though each of the pixel arrays in sample 2 are a minimum of 1575 members. Any help in understanding this?
edit: The “How to make it faster” section in the maxscript help has an example of line-by-line pixel editing which bears a slight resemblance to my sample 2 code, and I see I was doing some extraneous data collecting. Testing the modified code now, we’ll see what happens.
For anyone interested…
Sample code 3:
for y=1 to ambientPass.height do
(
backdropPixels = getPixels ambientPass [0,y-1] ambientPass.width
sourcePixels = getPixels occlusionPass [0,y-1] occlusionPass.width
for x=1 to ambientPass.width do
(
backdropPixels[x] = overlayBlend backdropPixels[x] sourcePixels[x]
)
setPixels comp1 [0,y-1] backdropPixels
)
That’s it. Time to complete is now ~7 minutes.