Ernie Rael wrote:
> Is it worth adding a putifabsent(dict, key, value) builtin function?
> Discuss among yourselves ;-)
>
> I occasionally need/want a dictionary->putifabsent(k, v). Either an
> existing key's value should not be changed, or the value would be the
> same. The second case offers a potential performance optimization.
> Should there be a builtin putifabsent()? Below are some profile
> results of the different ways to change a key's value.
>
> "d->extend({k: v})" is the only builtin way (that I know of).
> Besides "d->extend()" there's "if !d->has_key(k) | d[k] = v | endif",
> which is several times faster than d->extend(), but is cumbersome at
> best. A builtin, based on has_key() results, is 20% or more of an
> improvement over "if ..." and easy to type and understand. I was
> surprised to see a user function ~1,000 times slower.
Invoking a user function has a lot of overhead. You did use a compiled
function?
> The profiling, in micro-sec/op, is of putting a single k,v into a
> dictionary; the key is already in the target dictionary, column header
> is the number of keys in the target dictionary. There's something
> curious going on around 200 keys.
I don't think adding a function specifically to make something a bit
faster is a good idea. Using extend() should work, perhaps a little
profiling can show how to make it go faster. It might be that looping
over the dictionary to find the one entry adds some overhead (there are
always empty entries in the dictionary to make adding more items fast).
--
A man is incomplete until he's married ... and then he's finished!
/// 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/20230307171417.565BD1C0354%40moolenaar.net.