Novice question. I am looping through structure fields and would like to know how to keep this fast. Is there a way to work on these that is faster?
#Function that works on values function test(x,y,z) x=x.+1 y=y.+1 z=z.+1 return x,y,z end #Function that loops thorugh structure entries and does the same function test2(Structure) FieldsInStruct=fieldnames(typeof(Structure)); for i=1:length(FieldsInStruct) #Check field i Value=getfield(Structure, FieldsInStruct[i]) Value=Value.+1; setfield!(Structure,FieldsInStruct[i],Value) end end x=zeros(10); y=ones(10); z=ones(10).+1; mutable struct points; x;y;z; end PntLst=points(x,y,z) #Run two times... @time test(x,y,z) #0.000005 seconds (7 allocations: 640 bytes) @time test2(PntLst) #0.000036 seconds (14 allocations: 816 bytes)
Is there a way for me to get this second function faster, still without explictly typing out the structures field names?