Wednesday, August 30, 2023

Re: Vim9 script performance vs. legacy

Amazing! Thank you for doing this benchmarking. 

On Tue, Aug 29, 2023 at 6:38 PM Salman Halim <salmanhalim@gmail.com> wrote:


On Tue, Aug 29, 2023 at 10:33 AM Christian Brabandt <cblists@256bit.org> wrote:

On Di, 29 Aug 2023, Salman Halim wrote:

> Hello,
>
> I've been writing my scripts using Vim 9 recently (though without classes) and
> was wondering if anybody had any performance metrics/benchmarks they would be
> willing to share that compare Vim 9's speed compared to the same thing written
> in legacy code.
>
...
> Maybe the 'for' loop where I iterate over a list of strings is faster in Vim 9
> because it's both compiled and statically typed?
>
...
> What do people use for their own stuff these days?

I haven't done any measurements but I noticed converting to Vim9 script
helps, if you are calling your scripts many times.

So I have rewritten parts of vim-airline for those parts, that are
executed many many times and that are known to cause performance
degrations (see https://github.com/vim-airline/vim-airline/wiki/FAQ)

Best,
Christian

I took matters into my own hands and did a HIGHLY scientific test where I wrote these two functions that just add up the lengths of all the lines in the current file:

function! CountLengths()
  let total = 0

  for i in range( 1, line( '$' ) )
    let total += getline( i ) ->strchars()
  endfor

  return total
endfunction 

export def g:V9CountLengths(): number
  var total: number = 0

  for i in range( 1, line( '$' ) )
    total += getline( i ) ->strchars()
  endfor

  return total
enddef

I tried to keep the implementations as close to identical as possible. Then, I opened up a fairly large file (13 megs) where wordcount() returns:

{'chars': 13705032, 'cursor_chars': 1, 'words': 1515632, 'cursor_words': 1, 'bytes': 14136260, 'cursor_bytes': 1} <-- Guess where in the file I had my cursor!

I ran the legacy function ONCE (several times to verify) and got a time of 1.5754 seconds, which is definitely noticeable from when I pressed enter and when I got control back. I ran the Vim 9 version and it appeared to finish almost immediately, so I ran it TEN times in a loop and got a total time of 1.272675.

Thus, here, the Vim 9 function is more than twelve times faster. This result makes me happy and tells me that I should keep writing in Vim 9.

The only reason I can think of to stick with legacy is if people are now concerned about portability with Neovim if they're thinking that now, because of recent sad events, is a time to think about making a move.

For the curious, my timing function (legacy Vim, I know):

function! HowLong( command, numberOfTimes )
  " We don't want to be prompted by a message if the command being tried is an echo as that would slow things down while
  " waiting for user input.
  let more = &more
  set nomore

  let startTime = reltime()

  for i in range( a:numberOfTimes )
    execute a:command
  endfor

  let result = reltime( startTime )

  let &more = more

  return result
endfunction

And I tested it like this:

let g:result = HowLong('call CountLengths()', 1) ->reltimestr()
let g:result9 = HowLong('call V9CountLengths()', 10) ->reltimestr()

All the best,

Salman

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CANuxnEfC28j7D24rWj2h1EeS-SVaaTuJOQxLHPz2ZNfmLH_WPw%40mail.gmail.com.

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAOmRJrdVPVNyreQ%2BdHNDyHEVc7JLGHOud%2BLjg5xAPeVHB0ymKw%40mail.gmail.com.

No comments:

Post a Comment