11

Is there a programmatic way or eclipse plugin to calculate big-O notation for java method ?

Amgad Hanafy
  • 537
  • 5
  • 15
  • There may be a plugin, but if not then you can try keeping track of method calls and manually compute (or at least approximate) the running complexity. – Tim Biegeleisen Jul 17 '16 at 11:13
  • I haven't tried them but it seems [metrics](http://metrics.sourceforge.net/) and [checkstyle](http://eclipse-cs.sourceforge.net) plugins have cyclomatic complexity computation features – guleryuz Jul 17 '16 at 11:14
  • Keep in mind though, that when calculating complexity, you should be aiming for simple expression, e.g. O (n^2) rather than O (3n^2 + 2n +1) – Coderino Javarino Jul 17 '16 at 11:18
  • 2
    This isn't generally possible. In many cases proving the complexity of algorithms requires a lot of smarts that a program can't really provide. – Louis Wasserman Jul 17 '16 at 19:05
  • 1
    Why would anyone close this question? It seems like the kind of question you'd want asked and properly answered. Clearly the question shows a misunderstanding of asymptotic analysis but that doesn't mean it doesn't deserve a good answer that others could benefit from. @LouisWasserman - why don't you turn your comment into a slightly more developed answer? – Mike Dinescu Jul 18 '16 at 16:01
  • @CoderinoJavarino cyclomatic complexity and run-time complexity analysis are two very different metrics. As Louis Wasserman commented, determining the run-time complexity of an algorithm in all but the most basic of cases will require some abstract reasoning that cannot be achieved by a computer - see Halting Problem which is undecidable - and you can see why. – Mike Dinescu Jul 18 '16 at 16:05
  • @MikeDinescu I didn't imply that it should be calculated by computer. Quite the opposite actually - I said that in O(g(x)), g(x) should be roughly in the same ball park as the precise complexity function, be it algorithmically solvable or not. – Coderino Javarino Jul 18 '16 at 16:46
  • @CoderinoJavarino, I apologize, I meant to reply to the comment above yours – Mike Dinescu Jul 19 '16 at 04:00
  • @MikeDinescu If anybody tried codility challenges codility.com/programmers/challenges they are evaluating your solution against performance using big O notation , and I'm sure that they are not doing this calculation manually, that's why I'm asking this question. – Amgad Hanafy Jul 19 '16 at 11:08

1 Answers1

3

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).

Miljen Mikic
  • 14,765
  • 8
  • 58
  • 66