PDA

View Full Version : Maxscript optimisation 101

 reForm12-23-2008, 05:16 PM To save you reading through this thread, head over to http://www.tech-artists.org/wiki/Performance_Tests_(MAXScript) (http://www.tech-artists.org/wiki/Performance_Tests_%28MAXScript%29) where there are extensive code speed comparisons Hi, After watching some of Bobo's cgacademy video on The Matrix, it occured to me that there are quite a few functions I've been using that could have been scripted directly. With that thought I checked to see the speed comparison. My first test was using the cross product to calculate area rather than the meshop.getfacearea command. The results were surprising in one way, but I guess to be expected! With 10000 iterations: meshop.getfacearea = 0.281 seconds cross product method = 0.11 seconds That to me is a substantial difference. Maybe it would be helpful for everyone if we added scripted versions of standard maxscipt functions that offered similar speed advantages to this thread. Along with the geometric functions sticky I think this could be a handy resource. Here's the code for the area speed test that you can use as a basis for future speed tests. :) theobj=\$plane01 --You will need to make this plane and convert to editable_mesh! theface = getface theobj 1 thevert1 = getvert theobj theface.x thevert2 = getvert theobj theface.y thevert3 = getvert theobj theface.z thevector1 = thevert2 - thevert1 thevector2 = thevert3 - thevert1 iterations=100000 fn area1 = ( for i = 1 to iterations do thearea1 = (length (cross thevector1 thevector2))/2 ) fn area2 = ( for i = 1 to iterations do thearea2 = (meshop.getfacearea theobj 1) ) start = timeStamp() area1() -- do some big job end = timeStamp() format "Processing area1 took % seconds\n" ((end - start) / 1000.0) start = timeStamp() area2() -- do some big job end = timeStamp() format "Processing area2 took % seconds\n" ((end - start) / 1000.0)
RobGalanakis
12-24-2008, 02:31 PM
http://www.tech-artists.org/wiki/Performance_Tests_(MAXScript)

Sorry it is still a bit hidden from the main page, I have been putting off a major Wiki update for a while... can you add your info?

Bobo
12-24-2008, 02:51 PM
I am sorry, but this is the most misleading comparison I have ever seen.

In the area1 test, you just iterate and calculate cross products, but do NOT access any face or vertex data inside the loop, while poor meshop.getFaceArea() still has to do it in every iteration. (It does pretty much the same internally, but in C++ and is faster than the scripted version once both are set to do the same).

You cannot skip steps from the operation and claim that is faster ;)

Once you move all mesh access operations into the area1 function as it should be, it is slower - 1.5 seconds vs. 0.4 seconds on my home machine.

(
theobj=\$plane01 --You will need to make this plane and convert to editable_mesh!
iterations=100000
fn area1 =
(
for i = 1 to iterations do
(
theface = getface theobj 1
thevert1 = getvert theobj theface.x
thevert2 = getvert theobj theface.y
thevert3 = getvert theobj theface.z
thevector1 = thevert2 - thevert1
thevector2 = thevert3 - thevert1
thearea1 = (length (cross thevector1 thevector2))/2
)
)
fn area2 =
(
for i = 1 to iterations do
thearea2 = (meshop.getfacearea theobj 1)
)
start = timeStamp()
area1() -- do some big job
end = timeStamp()
format "Processing area1 took % seconds\n" ((end - start) / 1000.0)

start = timeStamp()
area2() -- do some big job
end = timeStamp()
format "Processing area2 took % seconds\n" ((end - start) / 1000.0)
)

reForm
12-24-2008, 10:26 PM
Aha Bobo, no need to apologise! Thanks for putting me straight :) although I'm hurt to hear this is the worst you've ever seen!

Still trying to make improvements in my maxscript skills but it seems I need to think more about my general logic too!

I've been enjoying your cgAcademy tutorials and look forward to your future productions.

reForm
12-24-2008, 10:27 PM
http://www.tech-artists.org/wiki/Performance_Tests_(MAXScript) (http://www.tech-artists.org/wiki/Performance_Tests_%28MAXScript%29)

Sorry it is still a bit hidden from the main page, I have been putting off a major Wiki update for a while... can you add your info?

Given the above post from Bobo I think I should refrain from polluting your excellent resource with my bad analysis :)

CGTalk Moderation
12-24-2008, 10:27 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.