> mg979 wrote:
> 
> > Test 1 finds an index inside a string long 100.000 characters, for 100.000 
> > times.
> > Test 2 and 3 iterate the same string once.
> 
> So test1 took 7.059 seconds and test3 took 0.005 seconds.
> 
> That is expected considering that `string[index]` is O(n) in Vim9
> (making test3 O(n^2)).
> Being O(n) was a design tradeoff as strings are stored in utf-8, which
> is variable size per characters.  An alternative would be to use
> utf-32, which would make it O(1), but it would consume more memory in
> general (4 times more memory for ASCII strings).

And it would introduce a new value type, and require all functions that
currently work on a string (which is very many) also to work with the new
type.  And the standard C functions working on strings can't be used,
I'm not sure how good wide string support is, many compilers only use 16
bits for wchar_t.

Using a list to store the characters also works.  It remembers the last
used item, looking up by an index close to a previously used index
is fast.  Creating the list has overhead though.

-- 
For large projects, Team Leaders use sophisticated project management software
to keep track of who's doing what.  The software collects the lies and guesses
of the project team and organizes them in to instantly outdated charts that
are too boring to look at closely.  This is called "planning".
                                (Scott Adams - The Dilbert principle)

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

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

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202109021450.182EoT9h288378%40masaka.moolenaar.net.

Raspunde prin e-mail lui