How can I improve this really slow code, which consists of several nested for loops?
Show older comments
Hi,
So I've written these nested for loops, but it takes a very long time to execute this code:
for T = linspace( 1, 5, 10 )
for xdot_0 = linspace( -3, 3, 10 )
for ydot_0 = linspace( 16, 20, 10 )
for thetadot_0 = linspace( -5, 0, 10 )
z = DifferenceMap( xdot_0, ydot_0, thetadot_0, T );
if norm(z) < 1e-3
disp( xdot_0, ydot_0, thetadot_0, T )
disp( z )
end
end
end
end
end
The DifferenceMap( ) function calls another function that gives solutions from the ode45 solver at final time, T.
Is there a better way to write this code so that it's faster?
Thanks,
4 Comments
Sindar
on 28 Sep 2020
it really depends on the specifics of DifferenceMap. A few possibilities:
- change one of the loops (probably outermost) to a parfor
- look into parallelizing DifferenceMap
- look into parallel versions of ode45 (no familiarity here, but I'd be surprised if there isn't anything)
Sindar
on 29 Sep 2020
There are definitely diminishing returns. parfor is easy to implement, but if you figure out lower-level parallelization (esp. vectorization), it has a good chance to be more effective
Accepted Answer
More Answers (0)
Categories
Find more on Parallel Computing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!