A few notes about code optimization
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.