| F(N) | Complexity Class |
|---|---|
| 1 | Constant |
| log(N) | Logarithm |
| N | Linear |
| Nlog(N) | N-Log-N (or linearithmic) |
| N2 | Quadratic |
| N3 | Cubic |
| 2N | Exponential |
| Complexity Class | F(N) | F(2N) | F(2N) / F(N) -- ratio |
|---|---|---|---|
| Constant | 1 | 1 | 1 |
| Linear | N | 2N | 2 |
| Quadratic | N2 | 4N2 | 4 |
| Cubic | N3 | 8N3 | 8 |
| Logarithm | log(N) | 1 + log(N) | 1 + 1/log(N) |
| N-Log-N (or linearithmic) | N*log(N) | 2N*log(N) + 2N | 2 + 2/log(N) |
| Exponential | 2N | 22N | 2N |
For example, F(2N) = (2N)2 = 4N2
Linear: n = 1000, time = 13859 Quadratic: n = 1000, time = 11861968 Linear: n = 2000, time = 24252 Quadratic: n = 2000, time = 47375115 Linear: n = 4000, time = 47734 Quadratic: n = 4000, time = 189609017 Linear: n = 8000, time = 96239 Quadratic: n = 8000, time = 757734300 Linear: n = 16000, time = 189397 Quadratic: n = 16000, time = 3030653875
Java Collection ArrayList LinkedList
======================================================
add(item) --------- constant/linear* constant
add(idx, item) ---- linear (**) linear
contains(item) ---- linear linear
remove(item) ------ linear linear
remove(idx, item)-- linear linear
get(idx) ---------- constant linear
set(idx,item) ----- constant linear
iterator
hasNext-------- constant constant
next ---------- constant constant
remove -------- linear constant
*: Adding to the end of an arraylist is linear when the array is full and needs to be expanded
This method adds the new element x to the private array member at position index, but all the elements currently at position index, index+1, index+2, ... must be moved to the next higher position to make room for the new element x at position index.
This method is not so efficient. In particular, adding a new element at the beginning of the array:
add(0, x)
will have to move all the current elements in the array and so the time for this method is not constant, but will depend on the number of elements currently in the array. Thus its complexity is O(N).
The System class has a static method:
public static long currentTimeMillis()
which returns the number of milliseconds since the "epoch" (midnight, Jan 1, 1970).
By calling System.currentTimeMillis() before and after a call to the method to be timed, then computing the difference, you get a reasonable measure of the execution time of the method in milliseconds.
Disabling the JIT compiler
Timing with Eclipse
Eclipse interfes with timing. The problem is that Eclipse is using "just in time compiling" optimization during execution. Certain chunks of code will be "interpreted" one or more times, but then the jit compiler replaces the java byte code with native machine code. This means this chunk of code will run much faster if it is executed again during this run of the program. This obviously throws off any attempts to time execution.
For instructions on how to set the parameters in Eclipse, look at this page.
Executing from Command Line
java -cp . -Djava.compiler=NONE MyProg
BinarySearch -- O(lgn)
"2-Sum problem"
"3-Sum problem"