-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathtest.nim
52 lines (43 loc) · 1.35 KB
/
test.nim
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
import base64, times, strutils, strformat
import net
import posix
proc notify(msg: string) =
try:
let socket = newSocket()
defer: socket.close()
socket.connect("localhost", Port(9001))
socket.send(msg)
except:
discard
when isMainModule:
for (src, dst) in [("hello", "aGVsbG8="), ("world", "d29ybGQ=")]:
let encoded = base64.encode(src)
if encoded != dst:
stderr.writeLine(&"{encoded} != {dst}")
quit(1)
let decoded = base64.decode(dst)
if decoded != src:
stderr.writeLine(&"{decoded} != {src}")
quit(1)
const STR_SIZE = 131072
const TRIES = 8192
let str = strutils.repeat('a', STR_SIZE)
let str2 = base64.encode(str)
let str3 = base64.decode(str2)
var compiler = "Nim/clang"
when defined(gcc):
compiler = "Nim/gcc"
notify(&"{compiler}\t{getpid()}")
let t = times.epochTime()
var s_encoded = 0
for i in 1 .. TRIES:
s_encoded += len(base64.encode(str))
let t_encoded = formatFloat(times.epochTime() - t, ffDefault, 6)
let t1 = times.epochTime()
var s_decoded = 0
for i in 1 .. TRIES:
s_decoded += len(base64.decode(str2))
let t_decoded = formatFloat(times.epochTime() - t1, ffDefault, 6)
notify("stop")
echo(fmt"encode {str[0..3]}... to {str2[0..3]}...: {s_encoded}, {t_encoded}")
echo(fmt"decode {str2[0..3]}... to {str3[0..3]}...: {s_decoded}, {t_decoded}")