# Maxscript optimisation 101

 12 December 2008 reForm Expert portfolio Patrick Macdonald CG Artist reForm Studios London, United Kingdom Maxscript optimisation 101 To save you reading through this thread, head over to http://www.tech-artists.org/wiki/Performance_Tests_(MAXScript) 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) `````` __________________ Patrick Macdonald Lighting TD : http://reformstudios.com Developer of Mission Control , the spreadsheet editor for 3ds Max Last edited by reForm : 12 December 2008 at 11:26 AM. share quote
 12 December 2008 RobGalanakis Frequenter   portfolio Rob Galanakis Technical Animator Bioware Austin Austin, USA http://www.tech-artists.org/wiki/Pe...sts_(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? __________________ Tech-Artists.Org, a Community for Technical Artists www.robg3d.com Last edited by RobGalanakis : 12 December 2008 at 03:59 PM. Reason: had to fix url share quote
 12 December 2008 Bobo Krakatoa Guy   Borislav Petrov Product Specialist Thinkbox Software Vancouver, Canada 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) )`````` __________________ Bobo Last edited by Bobo : 12 December 2008 at 04:03 PM. share quote
 12 December 2008 reForm Expert portfolio Patrick Macdonald CG Artist reForm Studios London, United Kingdom 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. __________________ Patrick Macdonald Lighting TD : http://reformstudios.com Developer of Mission Control , the spreadsheet editor for 3ds Max share quote
 12 December 2008 reForm Expert portfolio Patrick Macdonald CG Artist reForm Studios London, United Kingdom Originally Posted by RobGalanakis: 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? Given the above post from Bobo I think I should refrain from polluting your excellent resource with my bad analysis __________________ Patrick Macdonald Lighting TD : http://reformstudios.com Developer of Mission Control , the spreadsheet editor for 3ds Max share quote
 12 December 2008 CGTalk Moderation Expert Thread automatically closed 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. __________________ CGTalk Policy/Legalities Note that as CGTalk Members, you agree to the terms and conditions of using this website. share quote

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off CGSociety Society of Digital Artists www.cgsociety.org Powered by vBulletinCopyright ©2000 - 2006, Jelsoft Enterprises Ltd.