robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
4.5 KiB217 linesraw
1local t = require('test.testutil')
2local describe, it = t.describe, t.it
3local N = 500
4local test_table_size = 100000
5
6describe('vim.iter perf', function()
7  local function mean(t)
8    assert(#t > 0)
9    local sum = 0
10    for _, v in ipairs(t) do
11      sum = sum + v
12    end
13    return sum / #t
14  end
15
16  local function median(t)
17    local len = #t
18    if len % 2 == 0 then
19      return t[len / 2]
20    end
21    return t[(len + 1) / 2]
22  end
23
24  -- Assert that results are equal between each benchmark
25  local last = nil
26
27  local function reset()
28    last = nil
29  end
30
31  local input = {}
32  for i = 1, test_table_size do
33    input[#input + 1] = i
34  end
35
36  local function measure(f)
37    local stats = {}
38    local result
39    for _ = 1, N do
40      local tic = vim.uv.hrtime()
41      result = f(input)
42      local toc = vim.uv.hrtime()
43      stats[#stats + 1] = (toc - tic) / 1000000
44    end
45    table.sort(stats)
46    print(
47      string.format(
48        '\nMin: %0.6f ms, Max: %0.6f ms, Median: %0.6f ms, Mean: %0.6f ms',
49        math.min(unpack(stats)),
50        math.max(unpack(stats)),
51        median(stats),
52        mean(stats)
53      )
54    )
55
56    if last ~= nil then
57      assert(#result == #last)
58      for i, v in ipairs(result) do
59        if type(v) == 'string' or type(v) == 'number' then
60          assert(last[i] == v)
61        elseif type(v) == 'table' then
62          for k, vv in pairs(v) do
63            assert(last[i][k] == vv)
64          end
65        end
66      end
67    end
68
69    last = result
70  end
71
72  describe('list like table', function()
73    describe('simple map', function()
74      reset()
75
76      it('vim.iter', function()
77        local function f(t)
78          return vim
79            .iter(t)
80            :map(function(v)
81              return v * 2
82            end)
83            :totable()
84        end
85        measure(f)
86      end)
87
88      it('for loop', function()
89        local function f(t)
90          local res = {}
91          for i = 1, #t do
92            res[#res + 1] = t[i] * 2
93          end
94          return res
95        end
96        measure(f)
97      end)
98    end)
99
100    describe('filter, map, skip, reverse', function()
101      reset()
102
103      it('vim.iter', function()
104        local function f(t)
105          local i = 0
106          return vim
107            .iter(t)
108            :map(function(v)
109              i = i + 1
110              if i % 2 == 0 then
111                return v * 2
112              end
113            end)
114            :skip(1000)
115            :rev()
116            :totable()
117        end
118        measure(f)
119      end)
120
121      it('tables', function()
122        local function f(t)
123          local a = {}
124          for i = 1, #t do
125            if i % 2 == 0 then
126              a[#a + 1] = t[i] * 2
127            end
128          end
129
130          local b = {}
131          for i = 1001, #a do
132            b[#b + 1] = a[i]
133          end
134
135          local c = {}
136          for i = 1, #b do
137            c[#c + 1] = b[#b - i + 1]
138          end
139          return c
140        end
141        measure(f)
142      end)
143    end)
144  end)
145
146  describe('iterator', function()
147    describe('simple map', function()
148      reset()
149      it('vim.iter', function()
150        local function f(t)
151          return vim
152            .iter(ipairs(t))
153            :map(function(i, v)
154              return i + v
155            end)
156            :totable()
157        end
158        measure(f)
159      end)
160
161      it('ipairs', function()
162        local function f(t)
163          local res = {}
164          for i, v in ipairs(t) do
165            res[#res + 1] = i + v
166          end
167          return res
168        end
169        measure(f)
170      end)
171    end)
172
173    describe('multiple stages', function()
174      reset()
175      it('vim.iter', function()
176        local function f(t)
177          return vim
178            .iter(ipairs(t))
179            :map(function(i, v)
180              if i % 2 ~= 0 then
181                return v
182              end
183            end)
184            :map(function(v)
185              return v * 3
186            end)
187            :skip(50)
188            :totable()
189        end
190        measure(f)
191      end)
192
193      it('ipairs', function()
194        local function f(t)
195          local a = {}
196          for i, v in ipairs(t) do
197            if i % 2 ~= 0 then
198              a[#a + 1] = v
199            end
200          end
201          local b = {}
202          for _, v in ipairs(a) do
203            b[#b + 1] = v * 3
204          end
205          local c = {}
206          for i, v in ipairs(b) do
207            if i > 50 then
208              c[#c + 1] = v
209            end
210          end
211          return c
212        end
213        measure(f)
214      end)
215    end)
216  end)
217end)