Skip to content

Instantly share code, notes, and snippets.

@cerealcable
Created July 26, 2011 22:38
Show Gist options
  • Select an option

  • Save cerealcable/1108260 to your computer and use it in GitHub Desktop.

Select an option

Save cerealcable/1108260 to your computer and use it in GitHub Desktop.
Mutator Test Class
package testapp;
public class Main {
public static void main(String[] args) {
MyClass o = new MyClass(1243.91);
Long start1 = System.nanoTime();
for(int i = 0; i < 10000; i++) {
Double value = o.getValue();
}
Long end1 = System.nanoTime();
Long start2 = System.nanoTime();
for(int i = 0; i < 10000; i++) {
Double value = o.value;
}
Long end2 = System.nanoTime();
Long start3 = System.nanoTime();
for(int i = 0; i < 10000; i++) {
o.setValue(22.22);
}
Long end3 = System.nanoTime();
Long start4 = System.nanoTime();
for(int i = 0; i < 10000; i++) {
o.value = 22.22;
}
Long end4 = System.nanoTime();
Long time1 = end1 - start1;
System.out.println("Access using method took " + time1);
Long time2 = end2 - start2;
System.out.println("Access using direct took " + time2);
Long time3 = end3 - start3;
System.out.println("Mutator using method took " + time3);
Long time4 = end3 - start3;
System.out.println("Mutator using direct took " + time3);
}
}
package testapp;
public class MyClass {
public Double value;
public MyClass() {
value = 0.0;
}
public MyClass(Double value) {
this.value = value;
}
public Double getValue() {
return value;
}
public void setValue(Double value) {
this.value = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment