3 ms·
> You've basically sorted those int-keyed elements into a list No, I've copied them into a list. There was no sorting. I've never compared elements or done any
by joshuamorton 3y ago
> You've basically sorted those int-keyed elements into a list
No, I've copied them into a list. There was no sorting. I've never compared elements or done any nlogn operations. I've shuffled, perhaps, or ordered, but not sorted. That's a meaningful distinction.
> have to keep track of missing elements, which is at least another loop through the list).
The implementation I have is complete, it errors if the array is sparse. You need at most two loops through the dict, which if you wanted to you could avoid with a small (constant time and constant space) change to the underlying dict implementation to cache the max integer key stored and require only a single iteration.
You can technically do even faster than that by making the iteration over the number of positional args, not total args, but in practice that would probably be slower.
And it's all going to be just as fast as kwargs today.
Like jumping back a bit your argument is
> Passing a dict is passing by reference, and if the called method reads 5 elements from that dict, it's tiny constant time.
But if you pass **kwargs to a method, the dict size is unbounded, so it's O(n), not constant! Exactly the same as now. And if the method being called declares it's params explicitly, it's still constant since you can immediately error on the first unknown parameter!
If it was constant before, it's still constant. If it was linear before, it's still linear. And you never need to sort anything.
Just to further clarify, above you say
> and **kwargs has every element read in O(1) time or it gets passed along by reference.
Reading every element in O(1) time is O(n)! And is precisely the same as what's happening now.
- necovek 3y agoIf you only need 5 kwargs, reading 5 elements from a 500k element dict is O(1). Just finding the max index is O(n). You might be right that even passing 500k element dict to a function **kwargs has terrible performance today anyway (I haven't checked but it does sound plausible from your timing tests). It seems like we are now looking for imprecisions in each other's reply and discussion does not seem to be constructive: we are agreeing on rough potential implementations, it's just that we differ whether that should be acceptable or not. That's fine, you are not convincing me and I am not convincing you, so we might as well drop it. It's not like this proposal is happening :) Have a good day!