public
Description: A library of higher-order functions for Lua
Homepage: http://www.samsarin.com/blog/lua-functional
Clone URL: git://github.com/samsarin/lua-functional.git
lua-functional / functional_perf.lua
100644 60 lines (49 sloc) 1.207 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
require 'functional'
 
functional.add_to_env(getfenv())
 
function test_map()
local a = {}
for i = 1, 10 do a[i] = i end
map(function(x) return x * 10 end, a)
end
 
function test_reduce()
local a = {}
for i = 1, 10 do a[i] = i end
reduce(function(t, v) return t + v end, 0, a)
end
 
function test_sort_by()
local a = {}
for i = 10, 1, -1 do a[i] = i end
sort_by(function(x) return x end, a)
end
 
function test_partial()
local func = function(x, y) return x + y end
local partial = partial(func, 5, PLACEHOLDER)
partial(10)
end
 
function test_curry()
local func = function(x, y) return x + y end
local curried = curry(func, 2, 5)
curried(10)
end
 
function test_filter()
local a = {}
for i = 1, 10 do a[i] = i end
filter(function(x) return x % 2 == 1 end, a)
end
 
function run_test(func, count)
local start = os.clock()
for i = 1, count do func() end
return os.clock() - start
end
 
tests = {
test_map = 100 * 1000,
test_reduce = 100 * 1000,
test_sort_by = 25 * 1000,
test_partial = 25 * 1000,
test_curry = 25 * 1000,
test_filter = 100 * 1000,
}
 
for test_name, count in pairs(tests) do
local result = run_test(_G[test_name], count)
print(test_name .. "(" .. count .. ")", result)
end