[Closed] is it smarter way for this ?
Hi
I have been trying again do some maxscript stuff and come to part of script where i am looking for some variable inside array which is created from point3 values and i would like to know if is any better, faster and smarter way how deal with this. My biggest issue is that i cant find any other way how search inside this values only by converting them into new integer array which i don't think is very efficient. So what do you think can you make this easier ? Thanks
my result
a = 5 -- searching for value 5
c =#()
d =#()
b = #([4,1,5],[3,6,5],[9,8,7]) -- array for search
for j = 1 to b.count do
(
c_temp = #()
d_temp = #()
for i = 1 to 3 do append c_temp (b[j][i] as integer) -- convert [] array into integer value
append c c_temp
d_temp = finditem c[j] a
append d d_temp
)
d
MAXScript is expression-based, so using intermediate variables to store the various stages of calculations, while allowed and possible, are not always necessary.
So you can reduce the whole searching part to a single complex expression:
a = 5 -- searching for value 5
b = #([4,1,5],[3,6,5],[9,8,7]) -- array for search
result = for j in b collect (finditem (for i = 1 to 3 collect j[i]) a)
The result will be the same #(3,3,0), but with a lot less intermediate results.
On the plus side, the code will run a bit faster if you are searching through a lot of Point3 values or if you execute the same code many times because intermediate variable allocations and memory management cost time. I tested your code vs. mine with 100,000 iterations and my timing was 7.688 seconds vs. 10.109 seconds for your code.
On the negative side, the above expression might be a lot more difficult to digest if you are seeing it for the first time…
hehe yes this is exactly what I was talking about. Quick and efficient how I wanted. I just didn’t know the right words :)Thanks Bobo
has to be a bit faster:
a = 5
b = #([4,1,5],[3,6,5],[9,8,7])
for p in b collect (finditem #(p[1],p[2],p[3]) a)
or ...
it looks ugly but it’s 3.5 times faster and 15 times less of memory leaking (vs original code):
a = 5
b = #([4,1,5],[3,6,5],[9,8,7])
for p in b collect (if p[1] == a then 1 else if p[2] == a then 2 else if p[3] == a then 3 else 0)
so, the smart way is not charming sometime.
Hi DenisT
I know i didn’t mention this in my post but I was thinking about much more way which you can use generally , because i guess your both version can be use only if you know how much numbers you have in array or for small array how I used in my example array and I must maybe mention that. But anyway thanks for another option how deal with collecting stuff quicker and efficient.
Cheers