Skip to content

Instantly share code, notes, and snippets.

@ichizok
Created October 10, 2013 07:27
Show Gist options
  • Save ichizok/6914387 to your computer and use it in GitHub Desktop.
Save ichizok/6914387 to your computer and use it in GitHub Desktop.
function! s:nop(x, y)
endfunction
function! s:dict_foldl_0(d)
for k in keys(a:d)
call s:nop(k, a:d[k])
endfor
endfunction
function! s:dict_foldl_1(d)
for [k, v] in items(a:d)
call s:nop(k, v)
endfor
endfunction
function! s:benchmark(func, sample, count)
let total = str2float('0')
echo a:func
echo '----------------'
for i in range(a:count)
let st = reltime()
call call(a:func, [a:sample])
let rt = reltime(st)
let total += str2float(printf('%d.%06d', rt[0], rt[1]))
echo printf('[%2d] %s', i+1, reltimestr(rt))
endfor
echo '----------------'
echo printf('total: %f, avg: %f', total, total/a:count)
endfunction
function! s:gen_dict(ks, vs)
let d = {}
for i in range(min([len(a:ks), len(a:vs)]))
let d[a:ks[i]] = a:vs[i]
endfor
return d
endfunction
function! s:bm_run()
let ks = range(1000)
let vs = map(copy(ks), 'range(v:val%10+1)')
let dct = s:gen_dict(ks, vs)
for func in [
\ 's:dict_foldl_0'
\ , 's:dict_foldl_1'
\ ]
call s:benchmark(func, dct, 10)
echo "================================\n"
endfor
endfunction
call s:bm_run()
function! s:nop(x)
endfunction
function! s:foldl(xs)
for x in a:xs
call s:nop(x)
endfor
endfunction
function! s:foldr_orig(xs)
for i in reverse(range(0, len(a:xs)-1))
call s:nop(a:xs[i])
endfor
endfunction
function! s:foldr_invr(xs)
for i in range(len(a:xs)-1, 0, -1)
call s:nop(a:xs[i])
endfor
endfunction
function! s:foldr_copy(xs)
call s:foldl(reverse(copy(a:xs)))
endfunction
function! s:benchmark(func, sample, count)
let total = str2float('0')
echo a:func
echo '----------------'
for i in range(a:count)
let st = reltime()
call call(a:func, [a:sample])
let rt = reltime(st)
let total += str2float(printf('%d.%06d', rt[0], rt[1]))
echo printf('[%2d] %s', i+1, reltimestr(rt))
endfor
echo '----------------'
echo printf('total: %f, avg: %f', total, total/a:count)
endfunction
function! s:gen_list(x, n)
let a = []
for i in range(a:n)
call add(a, a:x)
endfor
return a
endfunction
function! s:bm_run()
let lst = s:gen_list(s:gen_list('vim', 10), 1000)
for func in [
\ 's:foldr_orig'
\ , 's:foldr_invr'
\ , 's:foldr_copy'
\ ]
call s:benchmark(func, lst, 10)
echo "================================\n"
endfor
endfunction
call s:bm_run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment