Technology
 

Time your Vim commands

From Vim Tips Wiki

(Redirected from VimTip1232)

Tip 1232 Previous Next created May 15, 2006 · complexity intermediate · author muede · version 6.0


:command -complete=command -nargs=+ Time :let ct=strftime("%s") | exec <q-args> |let t=strftime("%s")| :echohl MoreMsg
 \|let min=(t - ct)/60 | let sec=(t - ct)%60
 \|let min = min < 10 ? "0".min : min | let sec= sec<10 ? "0".sec : sec | echo min.":".sec | echohl None

Possibly you need to change the strftime-string.

[edit] Comments

I usually was getting zero time used; that's probably what all the -1s are about. Here's an amended version using ten repeats. Obviously not all commands are suitable for use with ten repeats! So, feel free to modify maxrep to suit. As an example of use, consider

:Timer !ls

This is a version 7.0 script due to the printf().

com! -complete=command -nargs=+ Timer call s:Timer(<q-args>)
fun! s:Timer(cmd)
  " call Dfunc("Timer(cmd<".a:cmd.">)")
  let ct=strftime("%s")
  let maxrep= 10
  let repeat= 0
  while repeat < maxrep
    exec a:cmd
    let repeat= repeat + 1
  endwhile
  let t = strftime("%s")
  let dt = t - ct
  let min = (dt/maxrep)/60
  let sec = (dt/maxrep)%60
  let frac= (100*(dt%maxrep))/maxrep
  redraw!
  echohl MoreMsg
  echo printf("%d:%02d.%2d",min,sec,frac)
  echohl None
  " call Dret("Timer : t<".t.">")
endfun