Skip to content

Commit 401b42f

Browse files
committed
added Short
1 parent e15da52 commit 401b42f

File tree

2 files changed

+234
-70
lines changed

2 files changed

+234
-70
lines changed

README.MD

+61-43
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,53 @@ For each group I will discuss why its speed differs from the others.
2121
```
2222
name of benchmark avg time [ms] scaled score
2323
---------------------------------------------------------------------------
24-
shortSingleInt 0.155216 1.000000 (1)
25-
troveShort 0.157585 0.984971
26-
troveShortQuick 0.157680 0.984378
27-
shortMultiInt 0.161012 0.964007
28-
29-
intSingle 0.200699 0.773379 (2)
30-
troveIntQuick 0.201497 0.770317
31-
troveInt 0.201720 0.769465
32-
intMulti 0.202570 0.766235
33-
34-
shortSingleShort 0.279101 0.556130 (3)
35-
shortMultiShort 0.280039 0.554267
36-
37-
integerMultiInt 0.676243 0.229528 (4)
38-
integerSingleInt 0.678404 0.228796
39-
arrayListSingleInt 0.776887 0.199793
40-
ArrayListMutableSingleInt 0.803997 0.193056
41-
arrayListSingleintForeach 0.811861 0.191186
42-
ArrayListMutableSingleMutableIntForeach 0.813853 0.190718
43-
ArrayListMutableSingleIntForeach 0.820412 0.189193
44-
ArrayListMutableMultiIntTemp 0.831975 0.186564
45-
ArrayListMutableMultiMutableInt 0.836187 0.185624
46-
arrayListMultiIntTemp 0.846434 0.183377
47-
ArrayListMutableMultiIntForeach 0.848207 0.182994
48-
ArrayListMutableMultiInt 0.853029 0.181959
49-
ArrayListMutableMultiMutableIntForeach 0.855127 0.181513
50-
ArrayListMutableSingleMutableInt 0.857684 0.180972
51-
arrayListMultiIntForeach 0.880373 0.176308
52-
arrayListMultiInt 0.944912 0.164265
53-
54-
integerMultiInteger 2.139346 0.072553 (5)
55-
arrayListSingleIntegerForeach 2.241368 0.069251
56-
arrayListSingleInteger 2.367811 0.065553
57-
arrayListMultiIntegerForeach 2.740213 0.056644
58-
integerSingleInteger 2.818088 0.055079
59-
arrayListMultiInteger 2.924940 0.053067
60-
24+
name of benchmark avg time [ms] scaled score
25+
---------------------------------------------------------------------------
26+
shortSingleInt 0.143937 1.000000 (1)
27+
troveShort 0.144306 0.997443
28+
troveShortQuick 0.144464 0.996349
29+
shortMultiInt 0.146000 0.985868
30+
31+
intSingle 0.175314 0.821019 (2)
32+
troveIntQuick 0.180594 0.797019
33+
intMulti 0.181340 0.793740
34+
troveInt 0.181487 0.793094
35+
36+
shortSingleShort 0.268475 0.536127 (3)
37+
shortMultiShort 0.269197 0.534689
38+
39+
ArrayListMutableSingleInt 0.775027 0.185718 (4)
40+
ArrayListMutableSingleMutableInt 0.778915 0.184791
41+
ArrayListMutableSingleIntForeach 0.779429 0.184669
42+
ArrayListMutableSingleMutableIntForeach 0.779671 0.184612
43+
ArrayListMutableMultiIntTemp 0.795110 0.181027
44+
ArrayListMutableMultiInt 0.795508 0.180937
45+
ArrayListMutableMultiMutableInt 0.795630 0.180909
46+
ArrayListMutableMultiMutableIntForeach 0.800553 0.179796
47+
ArrayListMutableMultiIntForeach 0.800647 0.179775
48+
integerSingleInt 0.824274 0.174622
49+
integerMultiInt 0.897896 0.160304
50+
arrayListShortSingleInt 0.939230 0.153249
51+
arrayListShortSingleintForeach 0.945768 0.152190
52+
arrayListIntegerSingleInt 0.990180 0.145364
53+
arrayListIntegerMultiInt 1.009556 0.142574
54+
arrayListIntegerMultiIntTemp 1.010221 0.142480
55+
arrayListShortMultiIntTemp 1.020367 0.141063
56+
arrayListIntegerSingleintForeach 1.021236 0.140943
57+
arrayListShortMultiInt 1.022112 0.140823
58+
arrayListIntegerMultiIntForeach 1.025668 0.140334
59+
arrayListShortMultiIntForeach 1.029982 0.139747
60+
61+
integerSingleInteger 2.299529 0.062594 (5)
62+
integerMultiInteger 2.308570 0.062349
63+
arrayListShortSingleShort 2.324123 0.061932
64+
arrayListShortSingleShortForeach 2.376490 0.060567
65+
arrayListIntegerSingleInteger 2.440701 0.058973
66+
arrayListIntegerSingleIntegerForeach 2.468810 0.058302
67+
arrayListIntegerMultiIntegerForeach 2.695929 0.053390
68+
arrayListIntegerMultiInteger 2.763739 0.052080
69+
arrayListShortMultiShort 2.893809 0.049739
70+
arrayListShortMultiShortForeach 3.319872 0.043356
6171
```
6272

6373
##From 5 to 4
@@ -88,7 +98,9 @@ Integer a = 1, b = 2;
8898
a = Integer.valueOf(a.intValue() + b.intValue());
8999
```
90100
Apparently Integer is an immutable class, so instead of changing the int it is holding, a new object is allocated with the new int (I haven't looked further into why this is the case, but that is not the focus of these benchmarks).
91-
This explains the slowdown of a factor 2.2-2.5 quite well, as each addition causes the creation of a new object leaving the old object to be garbage collected.
101+
This explains the slowdown of a factor 2.2--4.3 quite well, as each addition causes the creation of a new object leaving the old object to be garbage collected.
102+
103+
For Short its even worse, its additional overhead compared to Integer is described in [Section 3 to 2](#3to2).
92104

93105
##From 4 to 2
94106
The first difference from 4 and 5 to 1,2 and 3 is that primitive data types are used instead of their corresponding wrapper classes.
@@ -97,6 +109,7 @@ This gives us two advantages:
97109
* The size of an int is 4 bytes, where an Integer takes up 32 bytes of heap space, so simply an array of ints has less data to be crunched. Check out http://btoddb-java-sizing.blogspot.dk/ for a more indepth analysis.
98110
* The method `intValue()` is no longer called to extract the int, saving an operation.
99111

112+
<a name="3to2"></a>
100113
##From 3 to 2
101114
When short is only half the size of an int, why does it perform worse?
102115
It is even accumulated in a short to avoid any conversion, well think again.
@@ -127,10 +140,15 @@ This one should now be obvious.
127140
To avoid the `i2s` operation the data type of the accumulator variable is changed to an int.
128141
We now get all the benefits of native addition, no method calling and less data to fetch from memory.
129142

130-
##Conclusion
143+
144+
##Memory
131145
As you might have noticed the use of ArrayLists or multidimensions haven't been mentioned so far as obvious sinners, because for my tests their usage didn't influenced the performance.
132-
Of course there is a little extra memory consumption of 2D arrays as each entry in the first dimension is a pointer to a second dimension.
133-
So an `Integer[2000][2000]` takes up 62,5 kb extra memory in contrast to a `Integer[2000*2000]`.
134-
Here the [Trove library](http://trove.starlight-systems.com/) offers high performance collections, e.g. ArrayLists that works on primitive data types instead of objects.
146+
Of course there is an extra memory consumption of 2D arrays as each entry in the first dimension is a pointer to a second dimension.
147+
148+
As far as I have been able to measure using some [code by Twitter](https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/objectsize/ObjectSizeCalculator.java) a 2D arrayList of 2000x2000 Integers consumes `46.9 kb` more memory than a 2D array of 2000 Integers, which consumes `39 kb` more memory than a 1D array of Integers.
149+
150+
The [Trove library](http://trove.starlight-systems.com/) offers high performance collections, e.g. ArrayLists that works on primitive data types instead of objects.
151+
152+
##Conclusion
135153

136-
Explore your language and if doubt which way to do it (disregardnig readability), benchmark to know which way is faster and disassemble to get insight *why* one way is faster.
154+
Explore your language and if doubt which way to do it (disregardnig readability), benchmark to know which way is faster and disassemble to get insight *why* one way is faster. Trying to be "smart" can end up hurting your performance

0 commit comments

Comments
 (0)