Vim Tips Wiki
Register
Advertisement
Tip 1232 Printable Monobook Previous Next

created 2006 · complexity intermediate · author muede · version 6.0


The original idea suffers from a typically too coarse time granularity of one second.

: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

This improved version needs Vim version 7.3 due to the printf() function, the use of floats and the reltime() function.

com! -count=0 -complete=command -nargs=+ Timer call s:Timer(<q-args>, <q-count>)
fun! s:Timer(cmd, count)
    let rel = has("reltime")
    let ct  = rel ? reltime() : localtime()
    let maxrep = a:count
    for i in range(maxrep)
        exec a:cmd
    endfor
    if rel
        let res = str2float(reltimestr(reltime(ct)))
    else
        let res = localtime() - ct + 0.0
    endif
    redraw!
    echohl MoreMsg
    if maxrep > 1
        echo printf("%*d rounds:\t%.02g sec", len(maxrep), maxrep, res)
    endif
    echo printf("%*d round :\t%.02g sec", len(maxrep), 1, res/maxrep)
    echohl None
endfun

Use an optional count argument, to specify how many iterations should be made, e.g. :100Timer :call funcFoobar() will call the function 100 times.

Comments[]

Advertisement