-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bench.lua
80 lines (70 loc) · 2.57 KB
/
Bench.lua
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
print"Data2String performance bench"
-- modified from [serpent](https://github.com/pkulchenko/serpent/blob/master/t/bench.lua).
local socket=require'socket'
local get_time=socket.gettime
and os.clock
local ITERS = ... or 10000
print("ITERS:",ITERS)
--print(table.unpack(arg,-2))
if not arg[-2]--[[execute outside from IDE, index 1 is interpreter]] then
os.execute("pause")
-- could set process priority then continue.
end
---------- test data ---------
local b = {text="ha'ns", ['co\nl or']='bl"ue', str="\"\n'\\\001"}
local a = {
x=1, y=2, z=3,
['function'] = b, -- keyword as a key
list={'a',nil,nil, -- shared reference, embedded nils
[9]='i','f',[5]='g',[7]={}}, -- empty table
['label 2'] = b, -- shared reference
[math.huge] = -math.huge, -- huge as number value
}
a.c = a -- self-reference
---------- serialize ----------
local Serializer = {
serpent = function(...) return require("serpent").dump(...) end,
penlight = function(...) return require("pl.pretty").write(...) end,
leopard = function(...) return require("leopard").Serialize(...) end,
D2S = function(...) return require'Data2String'(...) end,
D2S_compress = function(...) return require'Data2String'(...,'compress') end,
D2S_lazy = function(...) return require'Data2String'(...,'lazy') end,
D2S_lazy_compress = function(...) return require'Data2String'(...,'lazy compress') end,
--'penlight','leopard',
'serpent','D2S','D2S_compress','D2S_lazy','D2S_lazy_compress',
}
for index, serializer_name in ipairs(Serializer) do
--load require
local serializer=Serializer[serializer_name]
print(serializer_name,'\n',serializer(a))
end
for index, serializer_name in ipairs(Serializer) do
local serializer=Serializer[serializer_name]
local start, str = get_time()
for _ = 1, ITERS do str = serializer(a) end
print(("serializer %s (%d): %6.4fs"):format(serializer_name, ITERS, get_time() - start))
collectgarbage'collect';collectgarbage'collect'
end
--------- deserialize ---------
local Deserializers = {
serpent = load,
penlight = load,
D2S = load,
D2S_compress = load,
D2S_lazy = load,
D2S_lazy_compress = load,
'serpent','D2S','D2S_compress','D2S_lazy','D2S_lazy_compress',
}
for index, Serializer_Name in ipairs(Deserializers) do
local Serializer=Serializer[Serializer_Name]
local str = Serializer(a)
local copy
local start = get_time()
local Deserializer=Deserializers[Serializer_Name]
for _ = 1, ITERS do
copy=Deserializer(str)()
end
print(("deserializer %s (%d): %6.4fs"):format(Serializer_Name, ITERS, get_time() - start))
collectgarbage'collect';collectgarbage'collect'
end
print"finish"