I am developing a simulator but I dont have a lot experience and tricks about Julia. I have a case that seems like it has some kind of other way around that is much more efficient and optimized.
I will give you and example that is similar to my case.
struct Struct1
value::Float64
type::Symbol
end
obj1 = Struct1(1.0, :A)
obj2 = Struct1(1.0, :B)
obj3 = Struct1(1.0, :C)
objs = [obj1, obj2, obj3]
results = zeros(length(objs)*10)
for i in eachindex(objs)
obj = objs[i]
type = obj.type
for n in 1:10
position = (i-1)*10+n
############Here we have 20-40 lines of code block############
if type == :A
val = obj.value
results[position] = val # or much more complex operations in a few lines
elseif type == :B
val = obj.value
results[position] = val*2 # or much more complex operations in a few lines
elseif type == :C
val = obj.value
results[position] = val*3 # or much more complex operations in a few lines
end
end
end
Explanation:
Vector βobjsβ always stays constant. So types always will be same in that order. In that way, we always need to check if type is A or B or C for each iteration of loop 2 even we know that type will be always A through βloop 2β since we choose βobj1β at first iteration of βloop 1β.
one way to reduce meaningless condition checks 10 times in βloop2β is writing conditional statements before the βloop 2β:
for i in eachindex(objs)
obj = objs[i]
type = obj.type
if type == :A
val = obj.value
for n in 1:10
position = (i-1)*10+n
############Here we have 20-40 lines of code block############
results[position] = val # or much more complex operations in a few lines
end
elseif type == :B
val = obj.value
for n in 1:10
position = (i-1)*10+n
############Here we have 20-40 lines of code block############
results[position] = val # or much more complex operations in a few lines
end
elseif type == :C
val = obj.value
for n in 1:10
position = (i-1)*10+n
############Here we have 20-40 lines of code block############
results[position] = val # or much more complex operations in a few lines
end
end
end
But in this way we need to write a loop for each condition so we need to write " 20-40 lines of code block" for each which is not efficient. is there a way around?
Thank You