Use StringStream to build large strings

MAXScript FAQ > How To Make It Faster > Use StringStream to build large strings

If building strings, use a StringStream value to accumulate the string and then convert to a string.

Each string addition creates a new string.

FOR EXAMPLE:

a ="AAA"
b = a + a + a +a + a + a
--Creates 6 strings of length 3, 6, 9, 12, 15, 18 
a ="AAA"
b = (a + a + a) + (a + a + a)
--Creates 6 strings of length 3, 6, 9, 6, 9, 18 

TEST CASES:

fn test5a =
(
 local a =""
 for i = 1 to 100 do a += (i as string)
)
fn test 5b =
(
 local ss = stringstream""
 for i = 1 to 100 do format "%"(i as string) to:ss
 ss as string
)
fntest5c =
(
 local ss = stringstream"",fmt ="%"
 for i= 1 to 100 do format fmt (i as string) to:ss
 ss as string
)
fn test5d =
(
 local ss = stringstream"",fmt ="%"
 for i = 1 to 100 do format fmt ito:ss
 ss as string
)

FOR 100,000 ITERATIONS:

test5a()     -- 58875 msec., 505 MB
test5b()     -- 54672 msec., 39.2 MB
test5c()     -- 41125 msec., 29.2 MB
test5d()     -- 13532 msec., 10.0 MB

Previous Tip

Do not use return, break, exit or continue

Next Tip

Use name values instead of strings when possible