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