A few notes about code optimization

47 votes
Date Updated: 
01/31/2010
Author: 
Panayot Karabakalov (Anubis)

The base idea of this topic is to say to all coders - always to research, because this is the only way to get rid of any myths. Each myth may hinder your future development and may leave a wrong impression in us for the MaxScript language at all.

 

Well, probably, my short MaxScript experience (less than year) is not enough for a general conclusions about coding standards, but the code optimization, which is a very important part of the coding style (I hope not only for me), has always been exciting issue for me. Usually scripted languages are slower than the programming languages, and this is normal, but MaxScript is an exception then, uh? Yes, it can reach performance very closer to the SDK code, and if not, i.e. if the difference is significant, then the slow-down come from the coder, not from the MaxScript language itself. Yes, I believe in this.

 

So, the code optimization also means to fight against the myths.

The myths not born by itself, right? They always come from wrong interpretation. To get rid of them, we need to spend little time for research, i.e. first look into the help and if we still have some doubts then to make your own tests. Yes, this is important and is never a waste of time.

 

So, I'll sketch two examples.

First one is about "Select vs SelectMore" and the myth: "SelectMore is a slower function than Select"

Second one is about "If/Do vs If/Then" and the myth: "Never Use If/Do"

 

So, about the first one.

In the last MaxScript Reference edition (for 3ds Max 2010) was added new topic into "How To Make It Faster" section called "For Loop Collect Vs For Loop Do". This is a very useful acquisition. The topic cover exactly what it title said, it is not the "Select vs SelectMore". But I heard misinterpretation: "SelectMore is a slower function than Select". Well, I tested both functions with 100, 1000 and 10000 objects in 2 conditions (nothing selected, and some selected objects):

select objects
selectMore objects

...and in both cases selectMore "win"! Yes, both functions are mapped and used on object set selectMore() is a little bit more faster than Select().

So... myth busted! And this is a good example of how they (myths) born.

 

Now, what about "Never Use If/Do"?

Ok, first I need to say thank you to Jeff Hanna for his "MAXScript Coding Standards" topic here on ScriptSpot. Jeff Hanna assemble some rules which to help us to build your own coding style. With all respect to Jeff Hanna, I suspect that the rule "Never Use If/Do" is a myth too, and of course I'll say why I think so. In "If/Do" expression where "If" condition return False MAXWrapper don't read "Do" condition, while in "If/Then" expression MAXWrapper always read "Then" condition (in searching for optional "Else").

 

So, my personal conclusion is simple:
"If/Then" without "Else" is slower than "If/Do" (then the IF-condition return False).

 

Please let me know If something wrong in my conclusion. But just to be sure, of course I tested, so there is some example test:

a = 1
ts = timestamp()
for i=1 to 1000 do (if a != undefined then print a)
format "Time: %ms.\n" (timestamp()-ts)
--> Time: 2031ms. 
ts = timestamp()
for i=1 to 1000 do (if a != undefined do print a)
format "Time: %ms.\n" (timestamp()-ts)
--> Time: 2031ms.

Ok, they are equally if IF-condition is True. Now test again to see how is False condition works, i.e.:

a = undefined -- set a to undefined
-- and perform above test again

In my machine "If/Then" takes 69797ms. (about 70 seconds), and "If/Do" takes always 0ms. (Zero milliseconds!).

 

Briefly, I never use "If/Then" without supplying "Else", i.e. I use "If/Then/Else" or "If/Do". It's my style, and I don't say to follow it, however, I see no sense to use something which slow-down my script.

 

 

Best Regards

Anubis

Application Version: 
any 3ds Max version

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anubis's picture

Thanks Alexander for the

Thanks Alexander for the comments and tips.

With "Where" you come straight to the point, it's my favourite expression in "For" loops.

Cheers
Anubis

my recent MAXScripts RSS (archive here)

Kstudio's picture

Thanks for tutorial. I have a

Thanks for tutorial.

I have a few comments.

About Selection.
For faster selection and faster viewport in max 2009 your can remove AutoCamMax.gup and AutoCamResENU.dll from stdglugs directory.
It is plugins used for ViewCube and SteeringWheel utilities.

About Expression.
Also if possible use "where" Expression.

(
a = 1
a = undefined -- set a to undefined
ts = timestamp()
-- for i=1 to 100000 do (if a != undefined then a+=i/25*pi)
for i=1 to 10000 do (if a != undefined then select objects[1];clearselection())
-- for i=1 to 1000 do (if a != undefined then  print a)
format "Time: %ms.\n" (timestamp()-ts)
-- print a
 
--> Time: 5094ms
 
a = 1
a = undefined -- set a to undefined
ts = timestamp()
for i=1 to 10000 do (if a != undefined do select objects[1];clearselection())
-- for i=1 to 1000 do (if a != undefined do print a)
format "Time: %ms.\n" (timestamp()-ts)
-- print a
--> Time: 4547ms.
 
a = 1
a = undefined -- set a to undefined
ts = timestamp()
for i=1 to 10000 where a != undefined do select objects[1];clearselection()
-- for i=1 to 1000 do (if a != undefined do print a)
format "Time: %ms.\n" (timestamp()-ts)
-- print a
--> Time: 0ms.
)

With "where" Expression i get time 0 ms.

Best Regards
Alexander Kramer

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.