How to reduce execution time in my code?

22 views (last 30 days)
Hi, I am pretty new using Matlab. I have created this code which is a model of a torsional pendulum that simulates bit rock interaction and vibrations in a drillstring. When my time step is 0.0001 (line 10) it goes OK. However I need to reduce my time step but it takes forever to run my code. I know I have some functions that take long but maybe there are some ways I could reduce the execution time by substituting some parts of it.
Could you suggest something? My code is attached.

Answers (1)

Adam
Adam on 25 Nov 2015
Edited: Adam on 25 Nov 2015
First step is to run the profiler and find out what is actually taking the time.
i.e.
profile on
eulerforwardsystemscaledtest
profile off
profile viewer
This gives an easily navigable interface telling you where in the code most time is spent. It is most useful on code that is built up from functions as it will tell you which functions take the time, but it will also tell you which lines of a script take the longest also, you just don't get the tree-structure to guide you like you do when working from functions.
Once you have pinpointed what is slow then you need to try to find alternative implementations. I usually do this by isolating the slow part of the problem and creating a file where I try implementing as many different ways of achieving the same result as I can think of, time each of them (and check they give the same results of course) and pick the best. Obviously this part relies on a good knowledge of Matlab (as well as your domain area) to come up with those alternative solutions, but you can always ask questions on speeding up specific bits of code after identifying them.
Vectorisation is one of the most powerful and common techniques for speeding up code, though I haven't looked at yours in detail to see if you are already doing this or if it is possible on your problem.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!