On 15-Mar-2010 18:57, ZyX wrote:
Ответ на сообщение <<Re: col() in lines with apostrophe characters>>, присланное в 20:15:39 14 марта 2010, Воскресенье, отправитель Ingo Karkat:let len = strlen(substitute(str, ".", "x", "g"))Never use this. It is three orders of magnitude slower than let len=len(split(str, '\zs')) You can test this by launching vim -u NONE -c 'source test.vim' My results are located in <<strprofile>> file. Note that both methods assume that one symbol is symbol+combining diacritics (if any).
Thank you for this valuable insight. I had taken my substitution straight from Vim's :help strlen().
When re-producing your profiling on my (Linux x86, Vim 7.2.368) system, the difference is one order of magnitude (0.84 vs 0.08), still substantial.
Your test processes a very long string once. When I changed this to processing a short string many times over (test2.vim, attached; an arguably more real-life scenario), the substitution eventually is faster than the split.
I intended to ask Bram to update the documentation with your improved algorithm. With my inconclusive results, I'm not so sure any more.
-- regards, ingo -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php
FUNCTION Test1() Called 1 time Total time: 0.840012 Self time: 0.840012 count total (s) self (s) 1 0.840012 return len(substitute(a:str, '.', '.', 'g')) FUNCTION Test2() Called 1 time Total time: 0.080001 Self time: 0.080001 count total (s) self (s) 1 0.080001 return len(split(a:str, '\zs')) FUNCTIONS SORTED ON TOTAL TIME count total (s) self (s) function 1 0.840012 Test1() 1 0.080001 Test2() FUNCTIONS SORTED ON SELF TIME count total (s) self (s) function 1 0.840012 Test1() 1 0.080001 Test2()
FUNCTION Test1() Called 1 time Total time: 2.970045 Self time: 2.970045 count total (s) self (s) 30001 0.060002 for i in s:range 30000 2.770042 let s:count += len(substitute(a:str, '.', '.', 'g')) 30000 0.080000 endfor 1 0.000000 call garbagecollect() FUNCTION Test2() Called 1 time Total time: 3.790057 Self time: 3.790057 count total (s) self (s) 30001 0.070002 for i in s:range 30000 3.490053 let s:count += len(split(a:str, '\zs')) 30000 0.140002 endfor 1 0.000000 call garbagecollect() FUNCTIONS SORTED ON TOTAL TIME count total (s) self (s) function 1 3.790057 Test2() 1 2.970045 Test1() FUNCTIONS SORTED ON SELF TIME count total (s) self (s) function 1 3.790057 Test2() 1 2.970045 Test1()
test2.vim
Description: Binary data