[Closed] Question optimizing 3 'for collect' loops
Im probably doing this the best way possible, but is a more efficient way to do this? It doesn’t seem efficient to do 3 loops to get the 2 arrays at the bottom that i’m looking for.
-- Search Code
similarNodes = for o in objects where matchpattern o.name pattern:(trim_objName +"*") collect o
similarXrefs = for o in similarNodes where (classof o == XRefObject) collect o
similarMeshes = for o in similarNodes where (classof o != XRefObject ) collect o
similarXrefs = #()
similarMeshes = #()
for o in objects where matchpattern o.name pattern:(trim_objName +"*") do
(
append (if iskindof o XRefObject then similarXrefs else similarMeshes) o
)
I’m not saying this is the absolute best it can be, but doesn’t loop as much
(if anyone else has a suggestion)
similarNodes = $trim_objName* as array
similarXrefs = #()
similarMeshes = #()
for o in similarNodes do
(
if (classof o == XRefObject) then
(
append similarXrefs o
)
else
(
append similarMeshes o
)
)
try to calculate time with
start = timestamp()
<code>
end = timestamp()
print (end-start)
trim_objName is a variable’s name, the value of the trim_objName is a string
to make your idea work:
similarNodes = (execute ("$'" + trim_objName + "'*")) as array
I tried to benchmark all these expressions while using about 10,000 objects and ALL code executed in about 550 ms, +/- 10 ms. So I don’t think either of these approaches is more effective in the sense of time it takes to execute, so in the end it is about style and readability. I don’t see a problem with the original code’s expressions since they describe quite clearly what is being done. I guess it is just a matter of taste…
on my personal taste i would keep the first loop and combine 2 and 3…
something like that:
_Xrefs = #()
_Meshes = #()
_Other = #()
_Nodes = for obj in objects where matchpattern obj.name pattern:(trim_objName +"*") collect obj
for node in _Nodes do case of
(
(iskindof node XRefObject): append _Xrefs node
(iskindof node GeometryClass): append _Meshes node
default: append _Other node
)