问题
Is there a programmatic way or eclipse plugin to calculate big-O notation for java method ?
回答1:
No, there isn't such plugin, and if it was, it would be a mere approximation. Namely, even determining whether the program will finish running or not is intractable - see Halting problem.
Now, about the possible approximation. Let's say you have a plugin that tests your program with a small dataset (e.g. N = 1000
) and a medium dataset (e.g. N = 10000
). If your program runs 10 times longer with a medium dataset compared to a small dataset, plugin should conclude that your program is O(N)
, right? Not quite. What about best/average/worst case? For example, quicksort's worst case is O(N^2)
, but it is generally considered as O(N*logN)
sorting algorithm. Therefore, if the plugin hits the special input, it will give a wrong result. What about constants? The program whose running time is O(N + k*logN)
is considered O(N)
, but if a constant k
is large enough compared to N
, plugin would not be able to reach this conclusion, etc.
Regarding your comment:
If anybody tried codility challenges they are evaluation your solution against performance using big O notation , and I'm sure that they are not calculation it manually, that's why I'm asking this question.
Authors of Codility challenges have solutions of their problems with a well-known time complexity (they analyzed it manually). When they measure the running time of your solution for various input and compare it with a running time of their solutions for the same input, they can automatically determine the time complexity of your program (of course, taking into account the programming language you have chosen and certain deviations of the measured time).
来源:https://stackoverflow.com/questions/38420564/is-there-a-programmatic-way-or-eclipse-plugin-to-calculate-big-o-notation-for-ja