Hi, guys and Happy New Year!
I am trying to go as fast a possible with this task - find words in string(text file).
This code snippet will generate a string(imitating text file) with 50 000 lines(the number is correct).
(
gc()
newLineStr = "\n"
newTabStr = "\t "
--( generate string
space = " "
nl = "\n"
tab = "\t"
tabNl = "\t\n"
fmt = "%\n"
wordsArr = #("alpha", "beta", "gama", "delta", "Epsilon", "Zeta", "Eta", "Theta", "Iota", "kaPPa", "LamBda", "mU", "Nu", "xi", "omicron", "pi", "rHo", "siGma", "Tau", "UpSiLoN", "pHi", "chi", "psi", "omega")
ss = stringStream ""
for i = 1 to 50000 do
(
wordsCnt = random 10 20
wArr = for j = 1 to wordsCnt collect (wordsArr[random 1 24])
tabCnt = random 0 5
str = ""
if mod i 17 == 0 then
str = tabNl
else
(
if mod i 33 == 0 then
str = nl
else
(
for t = 1 to tabCnt do str += tab
for w in wArr do str += space + w
)
)
format fmt str to:ss
)
--)
strToCheck = toLower (ss as string)
gc()
t0 = timestamp()
fStrArr = (filterString strToCheck newLineStr)
t1 = timestamp()
format "filterString % sec.\n" ((t1-t0)/1000.0)
format "Lines: %\n" fStrArr.count
_kappa = "kappa"
_omicron = "omicron"
_upsilon = "upsilon"
wordToFindArr = #(_kappa, _omicron, _upsilon)
kappaArr = #()
omicronArr = #()
upsilonArr = #()
gc()
t0 = timestamp()
for j = 1 to fStrArr.count where (stringArr = (filterString fStrArr[j] newTabStr)).count != 0 do
(
str1 = stringArr[1]
stopLoop = false
for i in wordToFindArr where i == str1 while stopLoop == false do
(
case str1 of
(
-- "collect line number"
_kappa:
(
append kappaArr j
stopLoop = true
)
-- "collcet the line text"
omicronArr:
(
append omicronArr (trimLeft fStrArr[j] space)
stopLoop = true
)
-- "collcet the line text"
upsilonArr:
(
append upsilonArr (trimLeft fStrArr[j] space)
stopLoop = true
)
)
)
)
t1 = timestamp()
format "Find words % sec.\n" ((t1-t0)/1000.0)
format "kappaArr: %\n" kappaArr.count
format "omicronArr: %\n" omicronArr.count
format "upsilonArr: %\n" upsilonArr.count
)
You can see what the script has to do - find each line of the strToCheck which starts with one of predefined words and collect some data.
With the code above I have:
Find words 0.285 sec.
if I remove the case str1 of statement the time is almost the same.
With the real text file I am using the time to find all predefined words is about: 0.7 sec.
If I use only python(outside 3ds max) the time to do the same is about 0.18 sec.
Is there are any way to make maxscript to perform the task faster?
The collected data will be used to fill a dotnet ListView.