Skip to content

Instantly share code, notes, and snippets.

@k-takata
Created June 12, 2014 12:38
Show Gist options
  • Save k-takata/406b39ed797c6b9f898c to your computer and use it in GitHub Desktop.
Save k-takata/406b39ed797c6b9f898c to your computer and use it in GitHub Desktop.
Performance of string concatenation (VimL)
let lines = readfile('vim/runtime/doc/eval.txt')
echo len(lines) . " lines"
" ----------------------------
" 1. Simplest string concatenation (.=)
let stime = reltime()
let line = ''
for l in lines
let line .= l
endfor
echo reltimestr(reltime(stime)) . " (" . strlen(line) . " bytes) " . "let line .= l"
unlet line
" 2. Use List with += and join()
let stime = reltime()
let ll = []
for l in lines
let ll += [l]
endfor
let line = join(ll, '')
echo reltimestr(reltime(stime)) . " (" . strlen(line) . " bytes) " . "let ll += [l]"
unlet line
unlet ll
" 3. Use List with add() and join()
let stime = reltime()
let ll = []
for l in lines
call add(ll, l)
endfor
let line = join(ll, '')
echo reltimestr(reltime(stime)) . " (" . strlen(line) . " bytes) " . "call add(ll, l)"
unlet line
unlet ll
" 4. Use List with add() with empty check and join()
let stime = reltime()
let ll = []
for l in lines
if l != ''
call add(ll, l)
endif
endfor
let line = join(ll, '')
echo reltimestr(reltime(stime)) . " (" . strlen(line) . " bytes) " . "call add(ll, l) with if"
unlet line
unlet ll
Vim 7.4.316
Linux:
8781 lines
0.327978 (330261 bytes) let line .= l
0.018020 (330261 bytes) let ll += [l]
0.021124 (330261 bytes) call add(ll, l)
0.028716 (330261 bytes) call add(ll, l) with if
Windows:
8781 lines
3.298255 (330261 bytes) let line .= l
0.028814 (330261 bytes) let ll += [l]
0.127366 (330261 bytes) call add(ll, l)
0.128387 (330261 bytes) call add(ll, l) with if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment