-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathtest.scala
More file actions
52 lines (42 loc) · 1.28 KB
/
test.scala
File metadata and controls
52 lines (42 loc) · 1.28 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
object Base64 {
val enc = java.util.Base64.getEncoder
val dec = java.util.Base64.getDecoder
def encode(str: Array[Byte]) = enc.encodeToString(str)
def decode(str: String) = dec.decode(str)
def notify(msg: String): Unit = {
scala.util.Using((new java.net.Socket("localhost", 9001)).getOutputStream()) {
_.write(msg.getBytes())
}
}
def main(args: Array[String]): Unit = {
val STR_SIZE = 131072
val TRIES = 8192
val str = Array.fill[Byte](STR_SIZE)('a')
println("JIT warming up")
for (_ <- 1 to 5) {
decode(encode(str))
}
notify(s"Scala\t${ProcessHandle.current().pid()}")
val t = System.nanoTime
var s = 0
var str2 = encode(str)
print(s"encode ${new String(str.take(4))}... to ${str2.substring(0, 4)}...: ")
for (_ <- 1 to TRIES) {
str2 = encode(str)
s += str2.length
}
println(s"$s, ${(System.nanoTime - t) / 1e9}")
var str3 = decode(str2)
print(s"decode ${str2.substring(0, 4)}... to ${new String(str3.take(4))}...: ")
s = 0
val t1 = System.nanoTime
for (_ <- 1 to TRIES) {
str3 = decode(str2)
s += str3.length
}
val now = System.nanoTime
println(s"$s, ${(now - t1) / 1e9}")
println(s"overall time: ${(now - t) / 1e9}s")
notify("stop")
}
}