Hi again, now with more time I could think better. You see, in that function I posted it would remove consecutive spaces and that might not be what you're after: something with five spaces between words would have them replaced with just one. It wouldn't be consistent with the input.
So the original function gives a better result. With that in mind, we can try other ways to optimize it (just for the heck of it; also because you're writing an exporter and need all the nanoseconds you can get).
Here is a little speed test with two different versions: the original and one that uses the 'Where' expression instead of an 'If'.
function CleanSpaces1 str_in = ( -- ORIGINAL
str_out = (copy str_in as string)
for i = 1 to (str_out.count as Integer) do
(
if(str_out == " ") do -- Changed to 'do'.
(
str_out = "_"
)
)
str_out
)
function CleanSpaces2 str_in = ( -- USING 'WHERE'
local str_out = copy (str_in as string)
size = str_in.count
for i = 1 to size where (str_out == " ") do (
str_out = "_"
)
str_out
)
start = timeStamp()
for i = 1 to 1500 do (
CleanSpaces1 "foo bar is nice."
)
format "Time taken for original: %" (timeStamp() - start)
start = timeStamp()
for i = 1 to 1500 do (
CleanSpaces2 "foo bar is nice."
)
format "Time taken for 'Where' version: %" (timeStamp() - start)
Everytime I evaluated (CTRL+E) the above, the "Where" version ran a few milliseconds faster.
Also, "If ... Then" expressions are slower than "If ... Do" ones (because they allow an "Else" to be added). So any time you need an "If" that doesn't require an "Else", use a "Do" for these single block conditions.
While there are a few MaxScript threads here, I can't recommend enough the CGSociety forums where they have
a whole subforum for the MaxScript language. Plenty of technical artists over there, it's not only a great place to ask questions but to read the discussions as well.
Make sure to read the
How To Make It Faster? article that comes with the MaxScript documentation, it explains several good practices and some very effective optimizations.