Forked from anonymous/SimpleInitiationTest.java
Last active
December 10, 2015 02:38
-
-
Save pablisco/4368924 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Test Setup | |
---------- | |
CPU: 2.2 GHz Intel Core i7 | |
RAM: 16 GB 1333 MHz DDR 3 | |
Disk: OCZ Vertex 4 128G | |
Java: Java SE 1.6 (MacOS X Default) | |
VM Arguments: -Xmx4096m | |
IDE: Eclipse 4.2.1 (M20120914-1800) | |
Tests: JUnit 4 | |
Test Aim | |
-------- | |
This test is assessing the performance hit on using double brace initiation on Java. This method of object creation can be handy to create object that have items inside or properties. A very practical use would be as a builder pattern where all the needed properties can be set inside (if set protected instead of private). It also helps separate object initiation. | |
Test Conclusions | |
---------------- | |
I was surprised to find that in most of the run tests the internal initiation was actually faster (almost double in some cases). When working with large numbers the benefit seems to fade away. | |
Interestingly, the case that creates 3 objects on the loop loses it's benefit rans out sooner than on the other cases. I am not sure why this is happening and more testing should be done to reach any conclusions. Creating concrete implementations may help to avoid the class definition to be reloaded (if that's what's happening) | |
However, it is clear that not much overhead it observed in most cases for the single item building, even with large numbers. | |
One set back would be the fact that each of the double brace initiations creates a new class file that adds a whole disk block to the size of our application (or about 1k when compressed). A small footprint, but if it's used in many places it could potentially have an impact. Use this 1000 times and you are potentially adding a whole MiB to you applicaiton, which may be concerning on an embedded environment. | |
My conclusion? It can be ok to use as long as it is not abused. | |
Source output | |
------------- | |
Folder | |
All classes: 17,298 bytes (49 KB on disk) for 9 items | |
No anonymous: 12,460 bytes (29 KB on disk) for 4 items | |
SimpleInitiationTest.class 4,757 bytes (8 KB on disk) | |
SimpleInitiationTest$1.class 934 bytes (4 KB on disk) | |
SimpleInitiationTest$2.class 940 bytes (4 KB on disk) | |
SimpleInitiationTest$3.class 940 bytes (4 KB on disk) | |
SimpleInitiationTest$4.class 940 bytes (4 KB on disk) | |
SimpleInitiationTest$5.class 1,084 bytes (4 KB on disk) | |
SimpleList.class 495 bytes (4 KB on disk) | |
SimpleObject.class 942 bytes (4 KB on disk) | |
Zipped: | |
All classes: 6,991 bytes (8 KB on disk) | |
No anonymous: 3,469 bytes (4 KB on disk) | |
Test Runs | |
--------- | |
1. TEST_COUNT = 1,000 | |
testExternalSimpleObjectInitiation 1763281041 | |
Time taken: 14ms | |
testInternalSimpleObjectInitiation 10969598 | |
Time taken: 14ms | |
testTripleExternalSimpleObjectInitiation 1412485250 | |
Time taken: 30ms | |
testTripleInternalSimpleObjectInitiation 906199566 | |
Time taken: 16ms | |
testExternalSimpleListInitiation 1062730578 | |
Time taken: 10ms | |
testInternalSimpleListInitiation 1709834834 | |
Time taken: 8ms | |
2. TEST_COUNT = 10,000 | |
testExternalSimpleObjectInitiation 1891275584 | |
Time taken: 83ms | |
testInternalSimpleObjectInitiation 1133736492 | |
Time taken: 18ms | |
testTripleExternalSimpleObjectInitiation 1336225759 | |
Time taken: 28ms | |
testTripleInternalSimpleObjectInitiation 1709834834 | |
Time taken: 31ms | |
testExternalSimpleListInitiation 1857040122 | |
Time taken: 28ms | |
testInternalSimpleListInitiation 302785728 | |
Time taken: 11ms | |
3. TEST_COUNT = 100,000 | |
testExternalSimpleObjectInitiation 1891275584 | |
Time taken: 179ms | |
testInternalSimpleObjectInitiation 911767762 | |
Time taken: 64ms | |
testTripleExternalSimpleObjectInitiation 301671635 | |
Time taken: 202ms | |
testTripleInternalSimpleObjectInitiation 322714437 | |
Time taken: 233ms | |
testExternalSimpleListInitiation 1793061098 | |
Time taken: 174ms | |
testInternalSimpleListInitiation 456335625 | |
Time taken: 132ms | |
4. TEST_COUNT = 1,000,000 | |
testExternalSimpleObjectInitiation 1763281041 | |
Time taken: 917ms | |
testInternalSimpleObjectInitiation 950233939 | |
Time taken: 753ms | |
testTripleExternalSimpleObjectInitiation 1450554350 | |
Time taken: 2043ms | |
testTripleInternalSimpleObjectInitiation 21845470 | |
Time taken: 2232ms | |
testExternalSimpleListInitiation 876433694 | |
Time taken: 1338ms | |
testInternalSimpleListInitiation 1506092990 | |
Time taken: 2164ms | |
5. TEST_COUNT = 10,000,000 | |
testExternalSimpleObjectInitiation 1763281041 | |
Time taken: 7662ms | |
testInternalSimpleObjectInitiation 911435008 | |
Time taken: 7478ms | |
testTripleExternalSimpleObjectInitiation 851272462 | |
Time taken: 18295ms | |
testTripleInternalSimpleObjectInitiation 1763538368 | |
Time taken: 27457ms | |
testExternalSimpleListInitiation 1180697045 | |
Time taken: 15792ms | |
testInternalSimpleListInitiation 747136299 | |
Time taken: 15549ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.List; | |
import java.util.Random; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class SimpleInitiationTest { | |
private static final int TEST_COUNT = 1000000; | |
private Random random = new Random(); | |
private long start; | |
@Before | |
public void setup() { | |
start = Calendar.getInstance().getTimeInMillis(); | |
} | |
@After | |
public void tearUp() { | |
long end = Calendar.getInstance().getTimeInMillis(); | |
System.out.println("Time taken: " + (end - start) + "ms"); | |
} | |
@Test public void testExternalSimpleObjectInitiation() { | |
System.out.println("testExternalSimpleObjectInitiation " + this.hashCode()); | |
List<SimpleObject> results = new ArrayList<SimpleObject>(TEST_COUNT); | |
for (int i = 0; i < TEST_COUNT; i++) { | |
SimpleObject victim = new SimpleObject(); | |
victim.setPage(random.nextInt()); | |
victim.setPageSize(random.nextInt()); | |
victim.setSomeData(Double.toString(random.nextDouble())); | |
results.add(victim); | |
} | |
} | |
@Test public void testInternalSimpleObjectInitiation() { | |
System.out.println("testInternalSimpleObjectInitiation " + this.hashCode()); | |
List<SimpleObject> results = new ArrayList<SimpleObject>(TEST_COUNT); | |
for (int i = 0; i < TEST_COUNT; i++) { | |
SimpleObject victim = new SimpleObject() { | |
{ | |
page = random.nextInt(); | |
pageSize = random.nextInt(); | |
someData = Double.toString(random.nextDouble()); | |
} | |
}; | |
results.add(victim); | |
} | |
} | |
@Test public void testTripleExternalSimpleObjectInitiation() { | |
System.out.println("testTripleExternalSimpleObjectInitiation " + this.hashCode()); | |
List<SimpleObject> results = new ArrayList<SimpleObject>(TEST_COUNT); | |
for (int i = 0; i < TEST_COUNT; i++) { | |
SimpleObject victim = new SimpleObject(); | |
victim.setPage(random.nextInt()); | |
victim.setPageSize(random.nextInt()); | |
victim.setSomeData(Double.toString(random.nextDouble())); | |
results.add(victim); | |
SimpleObject victim2 = new SimpleObject(); | |
victim.setPage(random.nextInt()); | |
victim.setPageSize(random.nextInt()); | |
victim.setSomeData(Double.toString(random.nextDouble())); | |
results.add(victim2); | |
SimpleObject victim3 = new SimpleObject(); | |
victim.setPage(random.nextInt()); | |
victim.setPageSize(random.nextInt()); | |
victim.setSomeData(Double.toString(random.nextDouble())); | |
results.add(victim3); | |
} | |
} | |
@Test public void testTripleInternalSimpleObjectInitiation() { | |
System.out.println("testTripleInternalSimpleObjectInitiation " + this.hashCode()); | |
List<SimpleObject> results = new ArrayList<SimpleObject>(TEST_COUNT); | |
for (int i = 0; i < TEST_COUNT; i++) { | |
SimpleObject victim = new SimpleObject() { | |
{ | |
page = random.nextInt(); | |
pageSize = random.nextInt(); | |
someData = Double.toString(random.nextDouble()); | |
} | |
}; | |
results.add(victim); | |
SimpleObject victim2 = new SimpleObject() { | |
{ | |
page = random.nextInt(); | |
pageSize = random.nextInt(); | |
someData = Double.toString(random.nextDouble()); | |
} | |
}; | |
results.add(victim2); | |
SimpleObject victim3 = new SimpleObject() { | |
{ | |
page = random.nextInt(); | |
pageSize = random.nextInt(); | |
someData = Double.toString(random.nextDouble()); | |
} | |
}; | |
results.add(victim3); | |
} | |
} | |
@Test public void testExternalSimpleListInitiation() { | |
System.out.println("testExternalSimpleListInitiation " + this.hashCode()); | |
List<SimpleList<String>> results = new ArrayList<SimpleList<String>>(TEST_COUNT); | |
for (int i = 0; i < TEST_COUNT; i++) { | |
SimpleList<String> victim = new SimpleList<String>(); | |
victim.add(Integer.toString(random.nextInt())); | |
victim.add(Integer.toString(random.nextInt())); | |
victim.add(Double.toString(random.nextDouble())); | |
results.add(victim); | |
} | |
} | |
@Test public void testInternalSimpleListInitiation() { | |
System.out.println("testInternalSimpleListInitiation " + this.hashCode()); | |
List<SimpleList<String>> results = new ArrayList<SimpleList<String>>(TEST_COUNT); | |
for (int i = 0; i < TEST_COUNT; i++) { | |
SimpleList<String> victim = new SimpleList<String>() { | |
private static final long serialVersionUID = 1L; | |
{ | |
add(Integer.toString(random.nextInt())); | |
add(Integer.toString(random.nextInt())); | |
add(Double.toString(random.nextDouble())); | |
} | |
}; | |
results.add(victim); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class SimpleList<T> extends ArrayList<T> { | |
private static final long serialVersionUID = 1L; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SimpleObject { | |
protected int page; | |
protected int pageSize; | |
protected String someData; | |
public int getPage() { | |
return page; | |
} | |
public void setPage(int page) { | |
this.page = page; | |
} | |
public int getPageSize() { | |
return pageSize; | |
} | |
public void setPageSize(int pageSize) { | |
this.pageSize = pageSize; | |
} | |
public String getSomeData() { | |
return someData; | |
} | |
public void setSomeData(String someData) { | |
this.someData = someData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment