How to say Java code performance

I just realized that I have no idea how to determine if a piece of Java code is computationally efficient. Reading a few source codes, sometimes I feel that the code I'm reading is very inefficient, sometimes I feel the opposite.

Could you list the basic rules of one-line for respect and why are they so important?

edit - My question is related to the Java JVM implementation, so things like Java allocation issues, String management, exception handling, thread synchronization, etc.

Thanks in advance

ps do not take "single line" literally pls

+3
source share
5 answers

Java , , Java. , Java.

+2

? , :

.

? , .: (

, , - ... , / .


, " " (, -, ), , , , , :

  • , , . ; , , , , .

  • / ( int Integer ..); Integer[], List<Integer> ,

  • ( , )

  • String; StringBuilder/StringBuffer. ( , / , .)

, .

+8

profiling. JProfile .

+7

jconsole , , . , .

enter image description here

+3

, " ".

, , , O. wikipedia Big O:

, , , . Big O : O .

Big-O , , . , :

void linearFoo(List<String> strings){
    for(String s:strings){
      doSomethingWithString(s);
    }
}

void quadraticFoo(List<String> strings){
    for(String s:strings){
        for(String s1:strings){
            doSomethingWithTwoStrings(s,s1);
        }
    }
}

linearFoo O (n), , n (.. strings.size()). quadraticFoo O (n 2), , , quadraticFoo, strings.size().

As soon as you feel the complexity of algorithmic time, your software profiling tools will become useful. For example, you can say that if during profiling you find that the method usually takes 1 ms for a fixed input size, if this method is O (n), doubling the input size will lead to a 2 ms runtime (1ms = n, therefore 2n = 2 ms). However, if it is O (n 2 ), doubling the size of the input will mean that your method will take about 4 ms to execute (1ms = n 2 therefore (2n) 2 = 4 ms).

+3
source

All Articles