Fandom

Vim Tips Wiki

Time your Vim commands

Redirected from VimTip1232

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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.

CommentsEdit

Also on Fandom

Random Wiki