-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathBase64Java.java
More file actions
64 lines (52 loc) · 1.98 KB
/
Base64Java.java
File metadata and controls
64 lines (52 loc) · 1.98 KB
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
import java.util.Arrays;
import java.util.Base64;
import static java.lang.System.out;
class Base64Java {
final static int STR_SIZE = 131072;
final static int TRIES = 8192;
final static Base64.Decoder dec = Base64.getDecoder();
final static Base64.Encoder enc = Base64.getEncoder();
private static void notify(final String msg) {
try (var socket = new java.net.Socket("localhost", 9001);
var out = socket.getOutputStream()) {
out.write(msg.getBytes("UTF-8"));
} catch (java.io.IOException e) {
// standalone usage
}
}
public static void main(String[] args){
final byte[] str = new byte[STR_SIZE];
Arrays.fill(str, (byte)'a');
out.println("JIT warming up");
for (int i = 0 ; i < 5 ; i++) {
dec.decode(enc.encodeToString(str));
}
notify("Java\t" + ProcessHandle.current().pid());
int s = 0;
final Long t = System.nanoTime();
String str2 = enc.encodeToString(str);
out.print(String.format("encode %s... to %s...: ",
new String(Arrays.copyOf(str, 4)),
str2.substring(0, 4)));
for (int i = 0 ; i < TRIES ; i++) {
str2 = enc.encodeToString(str);
s += str2.length();
}
out.println(String.format("%d, %.2f", s, (System.nanoTime() - t) / 1e9));
final byte[] encoded = str2.getBytes();
byte[] b_arr = dec.decode(encoded);
out.print(String.format("decode %s... to %s...: ",
str2.substring(0, 4),
new String(Arrays.copyOf(b_arr, 4))));
s = 0;
final Long t1 = System.nanoTime();
for (int i = 0 ; i < TRIES ; i++) {
b_arr = dec.decode(encoded);
s += b_arr.length;
}
final Long now = System.nanoTime();
out.println(String.format("%d, %.2f", s, (now - t1) / 1e9));
out.println("overall time: " + (now - t) / 1e9 + "s");
notify("stop");
}
}