5353 * EncodingHexJmhBenchmark.encodeStackOverflowCode 512 thrpt 4 917914,259 ± 122729,496 ops/s
5454 * EncodingHexJmhBenchmark.encodeStackOverflowCode 1000000 thrpt 4 471,778 ± 126,385 ops/s
5555 */
56+
57+ /**
58+ * Benchmark (byteLength) Mode Cnt Score Error Units
59+ * EncodingHexJmhBenchmark.encodeBigInteger 4 thrpt 2 9571230,509 ops/s
60+ * EncodingHexJmhBenchmark.encodeBigInteger 8 thrpt 2 3724335,328 ops/s
61+ * EncodingHexJmhBenchmark.encodeBigInteger 16 thrpt 2 1454898,799 ops/s
62+ * EncodingHexJmhBenchmark.encodeBigInteger 128 thrpt 2 135397,758 ops/s
63+ * EncodingHexJmhBenchmark.encodeBigInteger 512 thrpt 2 26011,356 ops/s
64+ * EncodingHexJmhBenchmark.encodeBigInteger 1000000 thrpt 2 4,163 ops/s
65+ * EncodingHexJmhBenchmark.encodeBytesLib 4 thrpt 2 24530310,446 ops/s
66+ * EncodingHexJmhBenchmark.encodeBytesLib 8 thrpt 2 23430124,448 ops/s
67+ * EncodingHexJmhBenchmark.encodeBytesLib 16 thrpt 2 20531301,587 ops/s
68+ * EncodingHexJmhBenchmark.encodeBytesLib 128 thrpt 2 6733027,370 ops/s
69+ * EncodingHexJmhBenchmark.encodeBytesLib 512 thrpt 2 1606857,133 ops/s
70+ * EncodingHexJmhBenchmark.encodeBytesLib 1000000 thrpt 2 772,010 ops/s
71+ * EncodingHexJmhBenchmark.encodeOldBytesLib 4 thrpt 2 18639952,166 ops/s
72+ * EncodingHexJmhBenchmark.encodeOldBytesLib 8 thrpt 2 15485869,934 ops/s
73+ * EncodingHexJmhBenchmark.encodeOldBytesLib 16 thrpt 2 11458232,999 ops/s
74+ * EncodingHexJmhBenchmark.encodeOldBytesLib 128 thrpt 2 2042399,306 ops/s
75+ * EncodingHexJmhBenchmark.encodeOldBytesLib 512 thrpt 2 280376,308 ops/s
76+ * EncodingHexJmhBenchmark.encodeOldBytesLib 1000000 thrpt 2 122,003 ops/s
77+ * EncodingHexJmhBenchmark.encodeStackOverflowCode1 4 thrpt 2 24755066,357 ops/s
78+ * EncodingHexJmhBenchmark.encodeStackOverflowCode1 8 thrpt 2 23455073,140 ops/s
79+ * EncodingHexJmhBenchmark.encodeStackOverflowCode1 16 thrpt 2 20548280,011 ops/s
80+ * EncodingHexJmhBenchmark.encodeStackOverflowCode1 128 thrpt 2 6675118,357 ops/s
81+ * EncodingHexJmhBenchmark.encodeStackOverflowCode1 512 thrpt 2 1618356,891 ops/s
82+ * EncodingHexJmhBenchmark.encodeStackOverflowCode1 1000000 thrpt 2 829,757 ops/s
83+ * EncodingHexJmhBenchmark.encodeStackOverflowCode2 4 thrpt 2 25323515,857 ops/s
84+ * EncodingHexJmhBenchmark.encodeStackOverflowCode2 8 thrpt 2 24027424,805 ops/s
85+ * EncodingHexJmhBenchmark.encodeStackOverflowCode2 16 thrpt 2 21262668,356 ops/s
86+ * EncodingHexJmhBenchmark.encodeStackOverflowCode2 128 thrpt 2 7492036,913 ops/s
87+ * EncodingHexJmhBenchmark.encodeStackOverflowCode2 512 thrpt 2 1789353,825 ops/s
88+ * EncodingHexJmhBenchmark.encodeStackOverflowCode2 1000000 thrpt 2 935,383 ops/s
89+ */
5690@ State (Scope .Thread )
5791@ Fork (1 )
58- @ Warmup (iterations = 2 , time = 2 )
59- @ Measurement (iterations = 4 , time = 5 )
92+ @ Warmup (iterations = 1 , time = 2 )
93+ @ Measurement (iterations = 2 , time = 5 )
6094@ BenchmarkMode (Mode .Throughput )
6195@ OutputTimeUnit (TimeUnit .SECONDS )
6296public class EncodingHexJmhBenchmark {
@@ -69,6 +103,7 @@ public class EncodingHexJmhBenchmark {
69103 private BinaryToTextEncoding .EncoderDecoder option2 ;
70104 private BinaryToTextEncoding .EncoderDecoder option3 ;
71105 private BinaryToTextEncoding .EncoderDecoder option4 ;
106+ private BinaryToTextEncoding .EncoderDecoder option5 ;
72107 private Random random ;
73108
74109 @ Setup (Level .Trial )
@@ -79,6 +114,7 @@ public void setup() {
79114 option2 = new BinaryToTextEncoding .Hex (true );
80115 option3 = new BigIntegerHexEncoder ();
81116 option4 = new OldBytesImplementation ();
117+ option5 = new StackOverflowAnswer2Encoder ();
82118
83119 rndMap = new HashMap <>();
84120 int [] lengths = new int []{4 , 8 , 16 , 128 , 512 , 1000000 };
@@ -91,26 +127,30 @@ public void setup() {
91127 }
92128 }
93129
94- @ Benchmark
95- public String encodeStackOverflowCode () {
130+ // @Benchmark
131+ public String encodeStackOverflowCode1 () {
96132 return encodeDecode (option1 );
97133 }
98134
99- @ Benchmark
135+ // @Benchmark
100136 public String encodeBytesLib () {
101137 return encodeDecode (option2 );
102138 }
103139
104- @ Benchmark
140+ // @Benchmark
105141 public String encodeBigInteger () {
106142 return encodeDecode (option3 );
107143 }
108144
109- @ Benchmark
145+ // @Benchmark
110146 public String encodeOldBytesLib () {
111147 return encodeDecode (option4 );
112148 }
113149
150+ @ Benchmark
151+ public String encodeStackOverflowCode2 () {
152+ return encodeDecode (option5 );
153+ }
114154
115155 private String encodeDecode (BinaryToTextEncoding .EncoderDecoder encoder ) {
116156 Bytes [] bytes = rndMap .get (byteLength );
@@ -141,6 +181,51 @@ public byte[] decode(CharSequence encoded) {
141181 }
142182 }
143183
184+ /**
185+ * With full lookup table of all 256 values
186+ * See: https://stackoverflow.com/a/21429909/774398
187+ */
188+ static final class StackOverflowAnswer2Encoder implements BinaryToTextEncoding .EncoderDecoder {
189+
190+ private static final char [] BYTE2HEX = (
191+ "000102030405060708090A0B0C0D0E0F" +
192+ "101112131415161718191A1B1C1D1E1F" +
193+ "202122232425262728292A2B2C2D2E2F" +
194+ "303132333435363738393A3B3C3D3E3F" +
195+ "404142434445464748494A4B4C4D4E4F" +
196+ "505152535455565758595A5B5C5D5E5F" +
197+ "606162636465666768696A6B6C6D6E6F" +
198+ "707172737475767778797A7B7C7D7E7F" +
199+ "808182838485868788898A8B8C8D8E8F" +
200+ "909192939495969798999A9B9C9D9E9F" +
201+ "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF" +
202+ "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF" +
203+ "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF" +
204+ "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF" +
205+ "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF" +
206+ "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF" ).toCharArray ();
207+
208+ @ Override
209+ public String encode (byte [] bytes , ByteOrder byteOrder ) {
210+ final int len = bytes .length ;
211+ final char [] chars = new char [len << 1 ];
212+ int hexIndex ;
213+ int idx = 0 ;
214+ int ofs = 0 ;
215+ while (ofs < len ) {
216+ hexIndex = (bytes [ofs ++] & 0xFF ) << 1 ;
217+ chars [idx ++] = BYTE2HEX [hexIndex ++];
218+ chars [idx ++] = BYTE2HEX [hexIndex ];
219+ }
220+ return new String (chars );
221+ }
222+
223+ @ Override
224+ public byte [] decode (CharSequence encoded ) {
225+ throw new UnsupportedOperationException ();
226+ }
227+ }
228+
144229 static final class BigIntegerHexEncoder implements BinaryToTextEncoding .EncoderDecoder {
145230 @ Override
146231 public String encode (byte [] bytes , ByteOrder byteOrder ) {
0 commit comments