Last active
February 5, 2018 15:11
-
-
Save tyru/d3640f241bcfb43eb43214fd6caf4b98 to your computer and use it in GitHub Desktop.
Benchmarks of Vital.Data.String.trim*() functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| " ================= trim ================= | |
| function! s:trim_matchstr_posix(s) abort | |
| return matchstr(a:s,'^[[:space:]]*\zs.\{-}\ze[[:space:]]*$') | |
| endfunction | |
| function! s:trim_matchstr(s) abort | |
| return matchstr(a:s,'^[ \t\r\n\v\f]*\zs.\{-}\ze[ \t\r\n\v\f]*$') | |
| endfunction | |
| function! s:trim_substitute_posix(s) abort | |
| return substitute(a:s,'^[[:space:]]\+\|[[:space:]]\+$', '', 'g') | |
| endfunction | |
| function! s:trim_substitute(s) abort | |
| return substitute(a:s,'^[ \t\r\n\v\f]\+\|[ \t\r\n\v\f]\+$', '', 'g') | |
| endfunction | |
| function! s:trim_fastest(s) abort | |
| return substitute(a:s,'\%#=1^[[:space:]]\+\|[[:space:]]\+$', '', 'g') | |
| endfunction | |
| " ================= trim_start ================= | |
| function! s:trim_start_matchstr(s) abort | |
| return matchstr(a:s,'^[[:space:]]*\zs.\{-}$') | |
| endfunction | |
| function! s:trim_start_substitute(s) abort | |
| return substitute(a:s,'^[[:space:]]\+', '', '') | |
| endfunction | |
| function! s:trim_start_slice(s) abort | |
| let i = match(a:s, '[^[:space:]]') | |
| return i is# -1 ? '' : a:s[i :] | |
| endfunction | |
| function! s:trim_start_fastest(s) abort | |
| return substitute(a:s,'\%#=1^[[:space:]]\+', '', '') | |
| endfunction | |
| " ================= trim_end ================= | |
| function! s:trim_end_matchstr(s) abort | |
| return matchstr(a:s,'^.\{-}\ze[[:space:]]*$') | |
| endfunction | |
| function! s:trim_end_substitute(s) abort | |
| return substitute(a:s,'[[:space:]]\+$', '', '') | |
| endfunction | |
| function! s:trim_end_slice(s) abort | |
| let i = match(a:s, '[[:space:]]*$') | |
| return i is# 0 ? '' : a:s[: i-1] | |
| endfunction | |
| function! s:trim_end_fastest(s) abort | |
| let i = match(a:s, '\%#=2[[:space:]]*$') | |
| return i is# 0 ? '' : a:s[: i-1] | |
| endfunction | |
| function! s:run() abort | |
| let results = [] | |
| for in in [ | |
| \ {'name': 'short', 'times': 100000, 'str': ' hello '}, | |
| \ {'name': 'long', 'times': 1000, 'str': repeat(' ', 1000) . 'hello' . repeat(' ', 1000)}, | |
| \] | |
| for &re in [0, 1, 2] | |
| " ================= trim ================= | |
| " s:trim_matchstr_posix() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_matchstr_posix(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_matchstr_posix(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| " s:trim_matchstr() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_matchstr(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_matchstr(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| " s:trim_substitute_posix() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_substitute_posix(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_substitute_posix(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| " s:trim_substitute() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_substitute(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_substitute(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| " ================= trim_start ================= | |
| " s:trim_start_matchstr() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_start_matchstr(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_start_matchstr(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| " s:trim_start_substitute() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_start_substitute(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_start_substitute(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| " s:trim_start_slice() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_start_slice(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_start_slice(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| " ================= trim_end ================= | |
| " s:trim_end_matchstr() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_end_matchstr(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_end_matchstr(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| " s:trim_end_substitute() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_end_substitute(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_end_substitute(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| " s:trim_end_slice() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_end_slice(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s,re=%d) [%dx] s:trim_end_slice(): %s', in.name, &re, in.times, split(reltimestr(diff))[0]), results) | |
| endfor | |
| " ================= trim ================= | |
| " s:trim_fastest() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_fastest(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s) [%dx] s:trim_fastest(): %s', in.name, in.times, split(reltimestr(diff))[0]), results) | |
| " ================= trim_start ================= | |
| " s:trim_start_fastest() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_start_fastest(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s) [%dx] s:trim_start_fastest(): %s', in.name, in.times, split(reltimestr(diff))[0]), results) | |
| " ================= trim_end ================= | |
| " s:trim_end_fastest() | |
| let start = reltime() | |
| for _ in range(in.times) | |
| call s:trim_end_fastest(in.str) | |
| endfor | |
| let diff = reltime(start) | |
| call s:tee(printf('(%s) [%dx] s:trim_end_fastest(): %s', in.name, in.times, split(reltimestr(diff))[0]), results) | |
| endfor | |
| redraw | |
| echo 'Written results to results.txt' | |
| call writefile(results, 'results.txt') | |
| endfunction | |
| function! s:tee(msg, list) abort | |
| call add(a:list, a:msg) | |
| redraw | |
| echomsg a:msg | |
| endfunction | |
| call s:run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (short,re=0) [100000x] s:trim_matchstr_posix(): 3.431844 | |
| (short,re=0) [100000x] s:trim_matchstr(): 3.625474 | |
| (short,re=0) [100000x] s:trim_substitute_posix(): 2.821455 | |
| (short,re=0) [100000x] s:trim_substitute(): 3.209605 | |
| (short,re=0) [100000x] s:trim_start_matchstr(): 2.884825 | |
| (short,re=0) [100000x] s:trim_start_substitute(): 1.551227 | |
| (short,re=0) [100000x] s:trim_start_slice(): 1.635898 | |
| (short,re=0) [100000x] s:trim_end_matchstr(): 3.090489 | |
| (short,re=0) [100000x] s:trim_end_substitute(): 2.456365 | |
| (short,re=0) [100000x] s:trim_end_slice(): 2.823873 | |
| (short,re=1) [100000x] s:trim_matchstr_posix(): 1.954353 | |
| (short,re=1) [100000x] s:trim_matchstr(): 2.125123 | |
| (short,re=1) [100000x] s:trim_substitute_posix(): 1.924148 | |
| (short,re=1) [100000x] s:trim_substitute(): 2.079971 | |
| (short,re=1) [100000x] s:trim_start_matchstr(): 1.504155 | |
| (short,re=1) [100000x] s:trim_start_substitute(): 1.115456 | |
| (short,re=1) [100000x] s:trim_start_slice(): 1.300202 | |
| (short,re=1) [100000x] s:trim_end_matchstr(): 1.872581 | |
| (short,re=1) [100000x] s:trim_end_substitute(): 1.512881 | |
| (short,re=1) [100000x] s:trim_end_slice(): 2.036221 | |
| (short,re=2) [100000x] s:trim_matchstr_posix(): 3.447780 | |
| (short,re=2) [100000x] s:trim_matchstr(): 3.587390 | |
| (short,re=2) [100000x] s:trim_substitute_posix(): 2.857396 | |
| (short,re=2) [100000x] s:trim_substitute(): 3.206429 | |
| (short,re=2) [100000x] s:trim_start_matchstr(): 2.878116 | |
| (short,re=2) [100000x] s:trim_start_substitute(): 1.545615 | |
| (short,re=2) [100000x] s:trim_start_slice(): 1.622258 | |
| (short,re=2) [100000x] s:trim_end_matchstr(): 3.066740 | |
| (short,re=2) [100000x] s:trim_end_substitute(): 2.451441 | |
| (short,re=2) [100000x] s:trim_end_slice(): 2.832053 | |
| (short) [100000x] s:trim_fastest(): 1.883539 | |
| (short) [100000x] s:trim_start_fastest(): 1.123300 | |
| (short) [100000x] s:trim_end_fastest(): 2.886493 | |
| (long,re=0) [1000x] s:trim_matchstr_posix(): 3.964880 | |
| (long,re=0) [1000x] s:trim_matchstr(): 3.970113 | |
| (long,re=0) [1000x] s:trim_substitute_posix(): 2.859825 | |
| (long,re=0) [1000x] s:trim_substitute(): 2.873896 | |
| (long,re=0) [1000x] s:trim_start_matchstr(): 3.080213 | |
| (long,re=0) [1000x] s:trim_start_substitute(): 1.207964 | |
| (long,re=0) [1000x] s:trim_start_slice(): 0.818772 | |
| (long,re=0) [1000x] s:trim_end_matchstr(): 3.331996 | |
| (long,re=0) [1000x] s:trim_end_substitute(): 3.272951 | |
| (long,re=0) [1000x] s:trim_end_slice(): 2.414008 | |
| (long,re=1) [1000x] s:trim_matchstr_posix(): 0.095643 | |
| (long,re=1) [1000x] s:trim_matchstr(): 0.051267 | |
| (long,re=1) [1000x] s:trim_substitute_posix(): 0.098056 | |
| (long,re=1) [1000x] s:trim_substitute(): 0.049016 | |
| (long,re=1) [1000x] s:trim_start_matchstr(): 0.146818 | |
| (long,re=1) [1000x] s:trim_start_substitute(): 0.049447 | |
| (long,re=1) [1000x] s:trim_start_slice(): 0.132879 | |
| (long,re=1) [1000x] s:trim_end_matchstr(): 53.262772 | |
| (long,re=1) [1000x] s:trim_end_substitute(): 54.655452 | |
| (long,re=1) [1000x] s:trim_end_slice(): 54.212232 | |
| (long,re=2) [1000x] s:trim_matchstr_posix(): 3.987011 | |
| (long,re=2) [1000x] s:trim_matchstr(): 3.981551 | |
| (long,re=2) [1000x] s:trim_substitute_posix(): 2.855004 | |
| (long,re=2) [1000x] s:trim_substitute(): 2.875990 | |
| (long,re=2) [1000x] s:trim_start_matchstr(): 3.071661 | |
| (long,re=2) [1000x] s:trim_start_substitute(): 1.233233 | |
| (long,re=2) [1000x] s:trim_start_slice(): 0.819886 | |
| (long,re=2) [1000x] s:trim_end_matchstr(): 3.329504 | |
| (long,re=2) [1000x] s:trim_end_substitute(): 3.374713 | |
| (long,re=2) [1000x] s:trim_end_slice(): 2.401908 | |
| (long) [1000x] s:trim_fastest(): 0.097177 | |
| (long) [1000x] s:trim_start_fastest(): 0.055331 | |
| (long) [1000x] s:trim_end_fastest(): 2.406792 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment