Dnia 23-12-2009 o godz. 13:19 Tim Chase napisał(a):
> 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.

Simple test on big file - trimming whitespaces from end of line:

%s/\s*$//
%s/\s\+$//

Basically this is special case of my second tip - * matches everywhere, 
\+ tests if action is necessary.

> 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.

True (and I thought obvious) but '=~' or stridx() works my way even with 
long strings or simple regexps (without * ;). IMO usually worth to try. 
This is other way of

:g/simpleregexp/s/complexregexp/replacement/

instead of

:%s/complexregexp/replacement/

Sad truth about such optimizations is that they often take more time to 
implement than actual savings during execution :D

m.

----------------------------------------------------
Wygraj nagrody za kupowanie prezentów - zobacz:
http://klik.wp.pl/?adr=http%3A%2F%2Fcorto.www.wp.pl%2Fas%2Ftaniokonkurs.html&sid=935


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

Reply via email to