[Closed] fast checking if array is empty
As I optimize code I noticed there is tons of checks if arrays are empty or not.
When I work on scene with thousands of objects I am running into trouble because code seems to execute slow.
By far I used to check it like this:
If array.count>0 then – execute rest of code but its slow I also have try this: If myarray !="#()" then -- this way wan
t work, don’t know why.
Is it possible to check if array is empty without counting items which is time consuming?
Thank You for any reply .
eh ? the size of an array should make no difference to the return speed of the arrays count property! mxs does not “count” the number of entries to get it’s value Its maintained by mxs when the user appends/deletes array elements or directly sets the count property eg
myarray.count = 100
I really doubt that this is the performance bottleneck in your code, but if it is, the solution is to change your algorithms to require less checks.
Yeah, growing and shrinking arrays are probably very bad ideas in general, if you want to write fast maxscript code.
i absolutely don’t understated your problem with an array count
ac = for k=1 to 100000 collect 1
t = timestamp()
c = 0
for k=1 to 10000 do c = ac.count
format "count:% time:% s
" c ((timestamp() - t)/1000.)
the execution time should be about 15ms
asking a count of an array via mxs doesn’t take a time! and it doesn’t matter what size of an array. it’s almost the same as call a variable
Pretty much like Klunk and Denis said, .count is definitely the fastest way. In theory it reads a variable from the class that gets updated every time you either add/delete items.