public
Last active — forked from /SimpleInitiationTest.java

  • Download Gist
SimpleInitiationTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
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);
}
}
 
}
SimpleList.java
Java
1 2 3 4 5 6 7
import java.util.ArrayList;
 
public class SimpleList<T> extends ArrayList<T> {
 
private static final long serialVersionUID = 1L;
 
}
SimpleObject.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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;
}
 
}
readme.rm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.