Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Profiling Vimscript performance

#39

Run time:

Vim users are unforgiving of plugins that impair performance. Luckily, Vim provides built-in profiling tools that make it easy to diagnose performance issues. We’ll start by looking at how to profile the vimrc file, then move on to a real world scenario where profiling helped to identify and aleviate a performance bottleneck.

Shownotes

In the video, I demonstrate how profiling enabled me to locate a bottleneck in my markdown-folding script. I used a 1500 line markdown file as a test case. The first attempt at a solution took 5 seconds to execute, the second solution ran in about 1 second, and the final solution took just 100 miliseconds. You can follow the same process by reading the discussion on github.

NOTE: Vim’s profiling feature is not enabled in all builds. You can check by running :version, and looking for the ‘profile’ feature. A plus sign prefix indicates that it is enabled.

Enable profiling on Vim startup

The --cmd and -c flags can be used to run Ex commands on Vim startup:

flag effect
--cmd Execute before loading any vimrc file
-c Execute after loading the first file

You could profile the load time for your vimrc file as follows:

vim --cmd 'profile start vimrc.profile' --cmd 'profile! file ~/.vimrc'

To profile a function in the vimrc file, you would have to call it. For example, if there was a function called Zzzz(), you could profile that function by running:

vim --cmd 'profile start vimrc.profile' --cmd 'profile! file ~/.vimrc' -c 'call Zzzz()'

Remember: the Ex commands specified with --cmd are run before loading the vimrc file, while Ex commands specified with -c are run after loading the first buffer.

Writing a simple script to profile Vim

In the example in the video, we could have profiled the Vimscript by launching Vim from the command line like this:

vim --cmd 'profile start MarkdownFolding-file.result' --cmd 'profile! file *markdown/folding.vim' -c 'profdel file *markdown/folding.vim' 1.5_kilos.md

We can make life a bit easier by wrapping this in a bash script:

#!/bin/bash
set -e
vim --cmd 'profile start MarkdownFolding-file.result' \
    --cmd 'profile! file *markdown/folding.vim' \
    -c 'profdel file *markdown/folding.vim' \
    1.5_kilos.md

Save this in a file called MarkdownFolding-file.runner, then it can be executed just by calling:

$ chmod +x MarkdownFolding-file.runner
$ ./MarkdownFolding-file.runner

This analyzes the markdown/folding.vim script, and saves the results to a file called MarkdownFolding-file.result.

Note the ! symbol in the line profile! file *markdown/folding.vim. Without the bang, Vim would only profile the loading of the script. The bang instructs Vim to also profile the execution of its functions. See :help profiling for more details.

In theory it should be safe to include the argument -c 'quit' in the profiling script, which would tell Vim to quit after loading the first buffer. In my case, I found that Vim would quit before executing the code that I wanted to profile. If anyone can suggest a way around this, I’d be interested to know more.

Further reading

Comments

Browse similar content


Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..