Created
June 16, 2014 12:55
-
-
Save k-takata/d53f6cf55edae7145310 to your computer and use it in GitHub Desktop.
Performance of map() and for loop (VimL)
This file contains 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
let hds = [] | |
for j in range(1, 200) | |
for i in range(0x20, 0x7e) | |
let hds += [printf("%02x", i)] | |
endfor | |
endfor | |
let hd = join(hds, '') | |
" Time of split | |
let stime = reltime() | |
let result = split(hd, '..\zs') | |
echo reltimestr(reltime(stime)) . " (split) " . len(result) | |
unlet result | |
" Time of split + map | |
let stime = reltime() | |
let result = map(split(hd, '..\zs'), '0') | |
echo reltimestr(reltime(stime)) . " (split + map) " . len(result) | |
unlet result | |
" split + map + join | |
let stime = reltime() | |
let result = join(map(split(hd, '..\zs'), 'v:val == "00" ? "" : eval(''"\x'' . v:val . ''"'')'), '') | |
echo reltimestr(reltime(stime)) . " (VimL 1) " . len(result) | |
unlet result | |
" split + map + join (without NUL check) | |
let stime = reltime() | |
let result = join(map(split(hd, '..\zs'), 'eval(''"\x'' . v:val . ''"'')'), '') | |
echo reltimestr(reltime(stime)) . " (VimL 2) " . len(result) | |
unlet result | |
" substitute | |
let stime = reltime() | |
let result = substitute(hd, '\x\x', '\=submatch(0)=="00"?"":eval(''"\x''.submatch(0).''"'')', 'g') | |
echo reltimestr(reltime(stime)) . " (VimL 3) " . len(result) | |
unlet result | |
" substitute (without NUL check) | |
let stime = reltime() | |
let result = substitute(hd, '\x\x', '\=eval(''"\x''.submatch(0).''"'')', 'g') | |
echo reltimestr(reltime(stime)) . " (VimL 4) " . len(result) | |
unlet result | |
" for loop | |
let stime = reltime() | |
let result = '' | |
let res = [] | |
for i in range(0, len(hd) / 2 - 1) | |
let res += [eval('"\x' . hd[i * 2 : i * 2 + 1] . '"')] | |
endfor | |
let result = join(res, '') | |
echo reltimestr(reltime(stime)) . " (VimL 5) " . len(result) | |
unlet result | |
" split + for loop | |
let stime = reltime() | |
let res = [] | |
for i in split(hd, '..\zs') | |
let res += [eval('"\x' . i . '"')] | |
endfor | |
let result = join(res, '') | |
echo reltimestr(reltime(stime)) . " (VimL 6) " . len(result) | |
unlet result | |
" Lua | |
let stime = reltime() | |
let result = [] | |
lua << EOF | |
do | |
local ret = vim.eval('result') | |
local hd = vim.eval('hd') | |
local len = string.len(hd) | |
local s = {} | |
for i = 1, len, 2 do | |
table.insert(s, string.char(tonumber(string.sub(hd, i, i+1), 16))) | |
end | |
ret:add(table.concat(s)) | |
end | |
EOF | |
echo reltimestr(reltime(stime)) . " (lua) " . len(result[0]) | |
unlet result |
This file contains 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
0.013292 (split) 19000 | |
0.212367 (split + map) 19000 | |
0.263043 (VimL 1) 19000 | |
0.243124 (VimL 2) 19000 | |
0.261827 (VimL 3) 19000 | |
0.241812 (VimL 4) 19000 | |
0.935701 (VimL 5) 19000 | |
0.106986 (VimL 6) 19000 | |
0.004395 (lua) 19000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment