PDA

View Full Version : dear bobo i have a question.


soshiant
11-16-2004, 01:30 PM
My question is about maxscripting, i mean the way we write codes,
Is it faster to put most of the code in 1 line of code?( because maxscript is an interpreter )
or devide it into more lines?
i remember a code like this:

a = (do some works on an IntegerValue) as string + "anotherString"... or sth like this

also we could break it into some lines.
you did the 1st, but someone said the readability is better when we break the code into lines.most of the times I need speed when Im working with maxscript.Im eager to read your reply.

I learn a lot from your posts, whenever i read them there are key points in them.
I wish I were you!!!
best wishes.

Bobo
11-16-2004, 05:26 PM
Thanks, dude :)

MAXScript is an expression-based language, similar to LISP, Mathematica etc.
It evaluates expressions which always return values, and builds a parser tree representing the structure of the code. This is why it is possible to write rather "functional" code without intermediate variables, where the result of one expressions is an argument of the next one, and you can nest them as deeply as you want.

When we say "divide in multiple lines", we should remember there are two ways of doing that:
*You can FORMAT the script on multiple lines without changing the code structure - either by using the existing formatting rules, or by employing the \ symbol.
*You can use intermediate variables to evaluate results of different expressions and then use these variables to further control the code flow.

We will be discussing the second variety here!

While you are right that readability of functional code might suffer, speed surely does not. You should remember that assigning a value to a variable means WRITING the value to MEMORY. This means that MAXScript has to allocate the memory for the variable and write the result to memory. In the next line, when the variable's value is needed, it has to go back and READ that memory it just wrote. So you basically have a write and a read compared to passing the return value directly to the next expression where you don't.

Here is a simple example:

*I created a Box01
*I created a second Box02 and added a Bend Modifier on top.
*I used the Array tool to create 100x10 copies of both boxes, which gave me 1000 objects with Bend and 1000 without, 2000 altogether.

Then I executed this script:

st = timestamp()
for i = 1 to 1000 do --repeat 1000 times to get usable results
(
BentArray = #() --init. an array to collect objects with Bend modifiers
for o in objects do --go through all objects
(
BendMods = #() --init. another array to colelct Bend modifiers
for m in o.modifiers do --go through all modifiers
if classof m == Bend do --if the modifier is a Bend,
append BendMods m --add modifier to the array
if BendMods.count > 0 do --if there were any Bend modifiers for the object,
append BentArray o --add object to array
)
)
et = timestamp()
print (et-st)

I executed this script which collected 1000 times 1000 of the 2000 objects.
It needed 12.047 seconds on my dual Athlon MP2800+
You can trust anyone to understand what the above code does.

Then I simplified the code into a less readable, but more immediate version:

st = timestamp()
for i = 1 to 1000 do
BentArray = for o in objects where (for m in o.modifiers where classof m == Bend collect m).count > 0 collect o
et = timestamp()
print (et-st)

This could be written even as a single line, or formatted on multiple lines, but it does not use intermediate variables! The only variable written is BentArray which is assigned the final result. The array of modifiers is never written to a variable, its count is compared directly to 0 and the result of true or false is given to the where expression as argument directly to make the decision whether to collect the object or not.

Execution time of this code was 10.218 seconds, almost 2 seconds faster!

In this particular case, 2 seconds are probably irrelevant. But if you are doing something with faces and vertices of an object that has millions of them, time would matter!

Writing to intermeditate variables is NOT ALWAYS BAD though!
As mentioned in the "How to make it faster" topic in the MAXScript Reference, using an intermediate variable to store a value that would be otherwise queried thousands of times is a Good Idea. So instead of saying


for f = 1 to theObject.mesh.numfaces do ...


it is MUCH better to say


theMesh = theObject.mesh
theCount = theMesh.numfaces
for f = 1 to theCount do...


In the first case, the for loop has to decide when to stop. Since it cannot be sure that the upper limit is not variable which might have changed after each iteration, it has to reevaluate the limit as often as the loop repeats. This means that you first get the TriMesh into memory (which on itself is a huge memory hit that can be very slow), then ask for the number of faces, and this is repeated as often as there are faces in the mesh!

In the second case, the mesh is snapped once, then the count is read once, and then the loop repeats as often as there are faces in the mesh, but the only evaluation happening to ensure the upper limit is reading the content of the variable theCount from memory, which is insignificant compared to reading the whole mesh into memory and then reading the count of faces all the time.

Btw, it is much faster to use
theMesh = snapshotAsMesh theObject
instead of
theMesh = theObject.mesh
in the above example...

Hope this brings some light into the topic...

Thanks for the good question!

Cheers,
Bobo

My question is about maxscripting, i mean the way we write codes,
Is it faster to put most of the code in 1 line of code?( because maxscript is an interpreter )
or devide it into more lines?
i remember a code like this:

a = (do some works on an IntegerValue) as string + "anotherString"... or sth like this

also we could break it into some lines.
you did the 1st, but someone said the readability is better when we break the code into lines.most of the times I need speed when Im working with maxscript.Im eager to read your reply.

I learn a lot from your posts, whenever i read them there are key points in them.
I wish I were you!!!
best wishes.

soshiant
11-17-2004, 02:05 PM
THANKS alot, i mean ALOT!:)

LFShade
11-17-2004, 04:06 PM
I considered answering when I first saw this question, but deferred to Bobo since it was, after all, directed to him. And I'm glad I waited, because my answer would have been simply this:

one-liners are faster, but be careful to comment them well - it's easy to come back to them later and forget what you intended!

A correct answer, and with a helpful caveat, but nowhere near as comprehensive, scholarly, and insightful as that of our dear mentor Bobo :D


RH

soshiant
11-21-2004, 01:32 PM
I considered answering when I first saw this question, but deferred to Bobo since it was, after all, directed to him. And I'm glad I waited, because my answer would have been simply this:

one-liners are faster, but be careful to comment them well - it's easy to come back to them later and forget what you intended!

A correct answer, and with a helpful caveat, but nowhere near as comprehensive, scholarly, and insightful as that of our dear mentor Bobo :D


RHThank you LFShade, as i said there are always important points in bobos posts, so this way I'll learn how can to code better in maxscript, bobo is one of those guys that i love him so much!( u 2 )thanx for your attention.

CGTalk Moderation
01-19-2006, 10:00 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.