Profiling Vim startup time

前端 未结 10 416
孤独总比滥情好
孤独总比滥情好 2020-12-07 07:16

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile it

相关标签:
10条回答
  • 2020-12-07 07:29

    You could run vim -V, pipe the output through a utility that adds timestamps and analyze the output. This command lines does this, e.g.:

    vim -V 2>&1 | perl -MTime::HiRes=time -ne 'print time, ": ", $_' | tee vilog
    

    You might have to blindly type :q to get back to your prompt. Afterwards, you should find the file vilog in your current directory with hires timestamps at the beginning of each line.

    If you can do with a granularity of a second, you can do this:

    vim -V 2>&1 | perl -ne 'print time, ": ", $_' | tee vilog
    
    0 讨论(0)
  • 2020-12-07 07:30

    You can use vim own profiling mechanism:

    vim --cmd 'profile start profile.log' \
        --cmd 'profile func *' \
        --cmd 'profile file *' \
        -c 'profdel func *' \
        -c 'profdel file *' \
        -c 'qa!'
    

    After running the above you will find a file called profile.log in the current directory with all required information. To get per-script information table similar to already present per-function one, use (after opening this file in vim):

    " Open profile.log file in vim first
    let timings=[]                      
    g/^SCRIPT/call add(timings, [getline('.')[len('SCRIPT  '):], matchstr(getline(line('.')+1), '^Sourced \zs\d\+')]+map(getline(line('.')+2, line('.')+3), 'matchstr(v:val, ''\d\+\.\d\+$'')'))
    enew                            
    call setline('.', ['count total (s)   self (s)  script']+map(copy(timings), 'printf("%5u %9s   %8s  %s", v:val[1], v:val[2], v:val[3], v:val[0])'))
    

    It will be unsorted, but you can always use built-in :sort command if number of scripts is too large.

    0 讨论(0)
  • 2020-12-07 07:31

    There is a plugin to profile the vim start-up time.

    http://www.vim.org/scripts/script.php?script_id=2915

    0 讨论(0)
  • 2020-12-07 07:34

    Based on the work done by @hyiltiz that depends on R, I made a Python version of the profiler, since this is more often available on a system that R.

    It's also slightly easier to extend, thus the features are:

    • Automatic detection of the plugin folder,
    • Bar plot thanks to matplotlib,
    • Run the analysis over several executions to get the average/standard deviation,
    • Supports both vim and neovim,
    • Can be used with a full vim command to test lazy-loading features, opening a file with a specific filetype etc.,
    • Export result to a csv file.

    The output is similar to what vim-plugins-profile provides:

    $ vim-profiler.py -p nvim
    
    Running nvim to generate startup logs... done.
    Loading and processing logs... done.
    Plugin directory: /home/user/.config/nvim/plugged
    =====================================
    Top 10 plugins slowing nvim's startup
    =====================================
    1         3.326   vim-fugitive
    2         2.936   tcomment_vim
    3         2.315   vim-hybrid
    4         1.751   lightline.vim
    5         0.959   vim-sneak
    6         0.943   supertab
    7         0.542   vim-surround
    8         0.536   fzf.vim
    9         0.450   fzf
    10        0.434   auto-pairs
    =====================================
    

    0 讨论(0)
  • 2020-12-07 07:37

    If you're using Vim 7.2.269 or later, then there's the --startuptime option you can use.

    vim --startuptime vim.log
    

    from the help (vim -h):

    --startuptime <file> Write startup timing messages to <file>
    
    0 讨论(0)
  • 2020-12-07 07:38

    If you're loading your plugins from a .vimrc file, what you could do is put a q on some line part way through the file to make it quit so you can use a process timer, like the unix time command. More thoroughly, this would look like:

    1. backup existing .vimrc file
    2. comment out all but a select number of plugins
    3. insert a q line
    4. call time vim repeteadly and average
    5. restore backup

    This is not elegant but I think it will get the job done.

    0 讨论(0)
提交回复
热议问题