code execution take time
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Guillaume
2018년 3월 6일
편집: Guillaume
2018년 3월 6일
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.
답변 (2개)
Guillaume
2018년 3월 6일
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
Guillaume
2018년 3월 6일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!