Skip to content

Latest commit

 

History

History
85 lines (73 loc) · 1.46 KB

efficientJava.md

File metadata and controls

85 lines (73 loc) · 1.46 KB

Efficient Programming for Java

Anti pattern

1. Array Copy

Bad

int[] newArray = new int[origin.length];
for (int i = 0; i < origin.length; i++) {
  newArray[i] = origin[i];
}

Good

import java.util.Arrays;
int[] newArray = Arrays.copyOf(origin, origin.length);

Note:

This is only for primitive array.

2. Array Access with for

Bad

for (int i = 0; i < array.length; i++) {
  array[i] = ...
}

Good

for (int i = 0, n = array.length; i < n; i++) {
  array[i] = ...    
}

3. Arithmetic Calculation

Bad

float a = (x * x - y * y) / (x * x + y * y);
float b = (x * x * y - y * y * x) / (x * x + y * y);

Good

float c = 1f / (x * x + y * y);
float d = x - y;
float a = (x + y) * d * c;
float b = x * y * d * c;

Note:

Speed : ADD ~ SUB > MULT > DIV

4. Autoboxing/Unboxing

Bad

public interface Calculation<V> {
  V getValue();
  void calc(Calculation<V> value);
}
public class Test implements Calculation<Float> {
  private float value;
  public Float getValue() {
    return this.value;
  }
  public void calc(Calculation<Float> value) {
    this.value = value.getValue() * value.getValue() * value.getValue();
  }
}

Good

public class Test... {
  public void calc(Calculation<Float> value) {
     Test temp = (Test)value;
     this.value = temp.value * temp.value * temp.value; 
  }
}

Note:

Autoboxing/Unboxing are useful for coding. But they will be a factor for bad performance.