Wednesday, December 23, 2009

Re: Best practice and enhance performance duration of vimscript

Mikołaj Machowski wrote:
>> I have all book on gvim but no one tells me the best practice to have
>> good performance when we write vimscript.
>
> 1. In regexps avoid when possible * wildcard.

I've never had problems with this, but it may affect long lines. It
might be possible to tweak the regex to use the \{-} "as few as
possible" operator to minimize forward searching in the FSM after the
first/shortest match is found.

> 2. When doing substitutions think if it is possible to check if
> substitution is really necessary - in long run expression
>
> if {is there something}
> substitute()
> endif
>
> is often faster than empty run of substitute().

Unless your test is something simple instead of a regep, the regex
engine ends up parsing the line twice, the first time for the test and
the second time for the substitute. So if you can make a quick
non-regex determination on the line like

if (line[0] == 'a')
s/complexregex/replacement/
endif

it will be faster. But checking for regex containment like

if line=~'complexregex'
s/complexregex/replacement/
endif

will just be slower.

> 3. :help profiling is your friend

definitely good advice :)

-tim


--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: