Skip to content

Instantly share code, notes, and snippets.

@lambdalisue
Created December 10, 2018 19:36
Show Gist options
  • Save lambdalisue/ab683aca933422654b4f6d8f4d37f8de to your computer and use it in GitHub Desktop.
Save lambdalisue/ab683aca933422654b4f6d8f4d37f8de to your computer and use it in GitHub Desktop.
compare old/new vital.data.list
let s:Benchmark = vital#vital#import('Vital.Benchmark')
let s:Old = vital#vital#import('Data.List')
let s:New = vital#vital#import('Vital.List')
function! s:map() abort
let list = repeat(['hello'], 1000)
echo 'map()'
echo printf(
\ ' Old: %f sec',
\ s:Benchmark.timeit({ -> s:OldList.map(list, { v -> toupper(v) }) })
\)
echo printf(
\ ' New: %f sec',
\ s:Benchmark.timeit({ -> s:NewList.map(list, { v -> toupper(v) }) })
\)
endfunction
function! s:filter() abort
let list = range(1000)
echo 'filter()'
echo printf(
\ ' Old: %f sec',
\ s:Benchmark.timeit({ -> s:OldList.filter(list, { v -> v % 2 is# 0 }) })
\)
echo printf(
\ ' New: %f sec',
\ s:Benchmark.timeit({ -> s:NewList.filter(list, { v -> v % 2 is# 0 }) })
\)
endfunction
function! s:compare(name, fn1, fn2) abort
let lhs = s:Benchmark.timeit(a:fn1)
let rhs = s:Benchmark.timeit(a:fn2)
echo printf("%s\t%f sec\t%f sec\t%f", a:name, lhs, rhs, lhs / rhs)
endfunction
echo "Fn\t\tOld \tNew \tRatio"
call s:compare(
\ "map\t",
\ { -> s:Old.map(range(1000), { v -> v * 2 }) },
\ { -> s:New.map(range(1000), { v -> v * 2 }) },
\)
call s:compare(
\ "filter\t",
\ { -> s:Old.filter(range(1000), { v -> v % 2 is# 0 }) },
\ { -> s:New.filter(range(1000), { v -> v % 2 is# 0 }) },
\)
call s:compare(
\ "reduce\t",
\ { -> s:Old.foldl1({ a, v -> a + v }, range(1000)) },
\ { -> s:New.reduce(range(1000), { a, v -> a + v }) },
\)
" foldr1 raise maxdepth exception for range(100)
call s:compare(
\ 'reduce_right',
\ { -> s:Old.foldr1({ a, v -> a + v }, range(10)) },
\ { -> s:New.reduce_right(range(10), { a, v -> a + v }) },
\)
call s:compare(
\ "flatten\t",
\ { -> s:Old.flatten(repeat([['hello']], 1000)) },
\ { -> s:New.flatten(repeat([['hello']], 1000)) },
\)
call s:compare(
\ "find_index",
\ { -> s:Old.find_index(range(1000), { v -> v % 32 is# 0 }) },
\ { -> s:New.find_index(range(1000), { v -> v % 32 is# 0 }) },
\)
call s:compare(
\ "find\t",
\ { -> s:Old.find(range(1000), v:null, { v -> v % 32 is# 0 }) },
\ { -> s:New.find(range(1000), { v -> v % 32 is# 0 }) },
\)
call s:compare(
\ "take_while",
\ { -> s:Old.take_while({ v -> v % 32 is# 0 }, range(1000)) },
\ { -> s:New.take_while(range(1000), { v -> v % 32 is# 0 }) },
\)
call s:compare(
\ "drop_while",
\ { -> s:Old.drop_while({ v -> v % 32 is# 0 }, range(1000)) },
\ { -> s:New.drop_while(range(1000), { v -> v % 32 is# 0 }) },
\)
echo ""
@lambdalisue
Copy link
Author


Fn		Old     	New     	Ratio
map		0.003990 sec	0.002387 sec	1.671902
filter		0.004531 sec	0.002570 sec	1.763037
reduce		0.005637 sec	0.004906 sec	1.148996
reduce_right	0.000150 sec	0.000054 sec	2.751017
flatten		0.017993 sec	0.014989 sec	1.200424
find_index	0.000256 sec	0.000133 sec	1.919957
find		0.000135 sec	0.000130 sec	1.034997
take_while	0.000149 sec	0.000147 sec	1.010756
drop_while	0.000280 sec	0.000278 sec	1.006264

*** time: 5.984777 ***

@aiya000
Copy link

aiya000 commented Dec 12, 2018

lambdalisue++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment