Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A tool for calculating the big-O time complexity of Java code? [closed]

I have a question regarding time complexity (big O notation) for Java software. Is there a way to quickly calculate or test it (or any website that could calculate it for me would be welcomed). For example I would like to check it for the following snippet of code and possibly improve as well:

int dcount = 24423567;
int a = 0;
if (dcount == 0){
    a = 1;
}

String ds = Integer.toString(dcount);
String[] sa = ds.split("(?<=.)");
HashSet hs = new HashSet();
Collections.addAll(hs, sa);
a = hs.size();
if (dcount < 0)
    a--;

System.out.println(a);
like image 754
aretai Avatar asked Mar 31 '12 17:03

aretai


People also ask

What is Big O complexity in Java?

Big O describes the set of all algorithms that run no worse than a certain speed (it's an upper bound) Conversely, Big Ω describes the set of all algorithms that run no better than a certain speed (it's a lower bound) Finally, Big Θ describes the set of all algorithms that run at a certain speed (it's like equality)

How do you calculate time complexity in Java?

The time complexity of a loop is equal to the number of times the innermost statement is to be executed. On the first iteration of i=0, the inner loop executes 0 times. On the first iteration of i=1, the inner loop executes 1 times. On the first iteration of i=n-1, the inner loop executes n-1 times.

Why Big O notation is a useful tool for comparing the efficiencies of algorithms?

Big-O notation counts how many steps an algorithm must execute to gauge its efficiency. Approaching your code in this manner can be very effective if you need to tune your code to increase efficiency.


1 Answers

As @emory pointed out, it is provably impossible to determine the big-O time complexity of an arbitrary piece of code automatically (the proof is a reduction from the halting problem). However, there are tools that can attempt to measure the complexity of a piece of code empirically by running it on several different inputs. One such tool is described in the paper “Measuring Empirical Computational Complexity” by Goldsmith, Aiken, and Wilkerson. It works by attempting to do a regression on the program's runtime versus its input size. The tool, called trend-prof, has been discontinued, but is archived here for reference.

like image 86
templatetypedef Avatar answered Nov 02 '22 04:11

templatetypedef