Two algorithms with the same complexity in Matlab
이전 댓글 표시
I was able to implement two algorithms and they have the same complexity in Matlab. One algorithm uses vectorized code and the other algorithm uses for loops. I computed the times for each algorithm for increasing sizes.
I noticed the following : 1) Algorithm 1 with vectorized code is much faster than algorithm 2 2) I plotted the times vs size for each algorithm and noticed they were both had N^2 growth however the curve for algorithm 2 grows much faster.
Can I conclude that in terms of complexity, both algorithms have the same complexity and ignore their actual running times?
I'm just measuring how the algorithm scales for different input sizes.
채택된 답변
추가 답변 (2개)
Jan
2013년 3월 16일
The runtime of an algorithm can follow a rule like:
runtime = a * n^2 + b * n^8
When b is sufficiently small, the runtime can look like depending on the size of the input n quadratically for sufficiently small problems. The real nature of the algorithm needs to be explored by a scientific analysis.
But such an deep analysis is not trivial, as it is not easy to define "complexity" accurately. Note, that an algorithm does not run independently from the hardware. For a growing problem, the sizes of the cacheline, 1st and 2nd level caches and even the size of the RAM, when disk swapping slows down the processing substantially.
Measuring the complexity of an algorithm by comparing the runtimes does not allow strong results.
Image Analyst
2013년 3월 15일
0 개 추천
I don't know how you're measuring complexity. Always or almost always the vectorized approach will be fewer lines of code. And it's usually, but not always, faster. Since it's faster for your code, just go with it. I'm not sure why you care only about the "complexity" and want to "ignore their actual running times". Just go with whatever is faster for the size of data you expect to encounter most.
댓글 수: 2
John
2013년 3월 15일
Image Analyst
2013년 3월 16일
You never answered Walter's questions, but since you accepted his answer, I assume that it answered your question and so this question is done.
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!