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
|
require "cjson"
require "posix"
function dump_value(value, indent)
if indent == nil then
indent = ""
end
if value == cjson.null then
value = "<cjson.null>"
end
if type(value) == "string" or type(value) == "number" or
type(value) == "boolean" then
print(indent .. tostring(value))
elseif type(value) == "table" then
local count = 0
for k, v in pairs(value) do
dump_value(v, indent .. k .. ": ")
count = count + 1
end
if count == 0 then
print(indent .. ": <empty>")
end
else
print(indent .. "<" .. type(value) .. ">")
end
end
function file_load(filename)
local file, err = io.open(filename)
if file == nil then
error("Unable to read " .. filename)
end
local data = file:read("*a")
file:close()
return data
end
function gettimeofday()
local tv_sec, tv_usec = posix.gettimeofday()
return tv_sec + tv_usec / 1000000
end
function benchmark(tests, iter, rep)
local function bench(func, iter)
collectgarbage("collect")
local t = gettimeofday()
for i = 1, iter do
func(i)
end
t = gettimeofday() - t
return (iter / t)
end
local test_results = {}
for name, func in pairs(tests) do
-- k(number), v(string)
-- k(string), v(function)
-- k(number), v(function)
if type(func) == "string" then
name = func
func = _G[name]
end
local result = {}
for i = 1, rep do
result[i] = bench(func, iter)
end
table.sort(result)
test_results[name] = result[rep]
end
return test_results
end
-- vi:ai et sw=4 ts=4:
|