On Nov 14, 2010, at 5:45 AM, ZyX wrote:

> Reply to message «Re: map copy and split v:val», 
> sent 11:09:31 14 November 2010, Sunday
> by epanda:
> 
>>> map(list1, 'v:val."*".list2[index(list1,v:val)]')
>> 
>> Ok this is perfect. It works like excel and faster I think...ust for
>> adding operations for the moment.
>> 
>> Thank you a lot Israel.
> No, that is a very bad solution: v:val may be contained in the list1 more 
> then 
> once and there is a huge impact on perfomance: `index(list1, v:val)' iterates 
> over list1 until it finds v:val, but it can be replaced with just v:key which 
> does not do any iterations and does not require all elements to be unique. 
> Here 
> is the benchmark:

I had no idea v:key could be used with lists too, good to know.

> 
> (zyx:tmp/vim/test-joinlists) % cat 1.vim
> let list1=range(1, 50000)
> let list2=copy(list1)
> call map(list1, 'v:val."*".list2[index(list1, v:val)]')
> (zyx:tmp/vim/test-joinlists) % cat 2.vim
> let list1=range(1, 50000)
> let list2=copy(list1)
> let i=0
> let llist=len(list1)
> while i<llist
>    let list1.='*'.list2[i]
>    let i+=1
> endwhile
> (zyx:tmp/vim/test-joinlists) % cat 3.vim
> let list1=range(1, 50000)
> let list2=copy(list1)
> call map(list1, 'v:val."*".list2[v:key]')
> (zyx:tmp/vim/test-joinlists) % cat 4.vim
> let list1=range(1, 50000)
> let list2=copy(list1)
> for i in range(0, len(list1)-1)
>    let list1[i].="*".list2[i]
> endfor
> (zyx:tmp/vim/test-joinlists) % for f in *.vim ; do time vim -u NONE -c 'set 
> nocompatible' -c "execute 'source '.fnameescape(${(qqq)f})" -c 'qa!' ; done
> vim -u NONE -c 'set nocompatible' -c  -c 'qa!'  8,27s user 0,01s system 99% 
> cpu 
> 8,287 total
> vim -u NONE -c 'set nocompatible' -c  -c 'qa!'  0,02s user 0,01s system 96% 
> cpu 
> 0,037 total
> vim -u NONE -c 'set nocompatible' -c  -c 'qa!'  0,17s user 0,01s system 98% 
> cpu 
> 0,182 total
> vim -u NONE -c 'set nocompatible' -c  -c 'qa!'  0,26s user 0,01s system 99% 
> cpu 
> 0,281 total
> 
> You see: while loop is the fastest solution, map() with v:key is slightly 
> slower 
> and solution with index is 50 times slower then map+v:key.

There is an error in your while loop file, you are concatenating to the list, 
which raises an error making it finish earlier. I re-ran the test with the 
corrected while loop:

% for f in *.vim ; do echo ; cat $f ; time vim -u NONE -N -c "execute 'source 
'.fnameescape(${(qqq)f})" -c 'qa!' ; done

let list1=range(1, 50000)
let list2=copy(list1)
call map(list1, 'v:val."*".list2[index(list1, v:val)]')
/Applications/MacVim.app/Contents/MacOS/Vim -u NONE -N -c  -c 'qa!'  7.09s user 
0.04s system 96% cpu 7.396 total

let list1=range(1, 50000)
let list2=copy(list1)
let i=0
let llist=len(list1)
while i<llist
   let list1[i].='*'.list2[i]
   let i+=1
endwhile
/Applications/MacVim.app/Contents/MacOS/Vim -u NONE -N -c  -c 'qa!'  0.44s user 
0.02s system 94% cpu 0.490 total

let list1=range(1, 50000)
let list2=copy(list1)
call map(list1, 'v:val."*".list2[v:key]')
/Applications/MacVim.app/Contents/MacOS/Vim -u NONE -N -c  -c 'qa!'  0.14s user 
0.01s system 94% cpu 0.158 total

let list1=range(1, 50000)
let list2=copy(list1)
for i in range(0, len(list1)-1)
   let list1[i].="*".list2[i]
endfor
/Applications/MacVim.app/Contents/MacOS/Vim -u NONE -N -c  -c 'qa!'  0.36s user 
0.02s system 96% cpu 0.392 total

Still, index() is the slowest by far, while v:key is the fastest by a good 
difference.

-- 
You received this message from the "vim_use" 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

Reply via email to