Vim Tips Wiki

Time your Vim commands

1,624pages on
this wiki
Add New Page
Talk0 Share
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
    if rel
        let res = str2float(reltimestr(reltime(ct)))
        let res = localtime() - ct + 0.0
    echohl MoreMsg
    if maxrep > 1
        echo printf("%*d rounds:\t%.02g sec", len(maxrep), maxrep, res)
    echo printf("%*d round :\t%.02g sec", len(maxrep), 1, res/maxrep)
    echohl None

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


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.

Also on Fandom

Random Wiki