code execution take time
1 view (last 30 days)
Show older comments
I run a code, which computes chaos degree in a series, the code whithin a loop take more than 7 hours. there is a way to perform this code, my lap top is very quick hp elitebook core i7 2.4 Ghz RAM 16GO. Some users told me that matlab take a lot of time with a loop, so i have to swith to C. There is a way to optimise time with matlab? If i have to switch to C, can i use the same code? Thinks
3 Comments
Guillaume
on 6 Mar 2018
Edited: Guillaume
on 6 Mar 2018
Are H, pValue, etc. preallocated? Otherwise the constant resizing will be one source of slow down.
edit: However, it's probably negligible compared to what's happening inside the function so not something to worry about for now, although preallocation is a practice that you should learn.
You're performing 5x6x5x(n-499) non-linear optimisations. Maybe that just takes time. As said in my answer you need to profile your code to know which part is actually slow. It's unlikely that the loops are a problem, it's just the amount of math that you're performing.
Note that unless you find a library that does non-linear optimisation, and all sort of matrix calculation, rewriting your code in C or any other language would be a major endeavour.
Answers (2)
Guillaume
on 6 Mar 2018
Loops may be slow or fast in matlab or in C. It all depends on whether or not the code has been written efficiently. An inefficient C program may well be slower than an efficient matlab program. Efficient C code may indeed be faster than efficient matlab but that is irrelevant if you don't know C.
C is a completely different language from matlab. You would have to rewrite your whole code and probably use a completely different design. It doesn't sound like you know C at all therefore I wouldn't even consider it as an option.
In any case, whenever you worry about the time it takes for code to run, the first thing you need to do is profile your code so that you know which bit is actually slow rather than taking guesses (which are likely to be wrong, or based on outdated assumptions).
Once you've located the slow portion then you can start optimising them. If you post the code we can probably help.
2 Comments
Guillaume
on 6 Mar 2018
For the purpose of profiling, the output of the function is not relevant. What's important is the result of the profiler, which shows how much time is spent performing each calculation.
See Also
Categories
Find more on Loops and Conditional Statements 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!