3 ms·
What is the complexity of set operations on arrays? This seems a bit too much sugar to me, to the point where people will end up writing inefficient code becaus
by saagarjha 8y ago
What is the complexity of set operations on arrays? This seems a bit too much sugar to me, to the point where people will end up writing inefficient code because they will thing these methods are more performant than they really are. Usually, I like to see explicit conversions to sets to make the performance characteristics explicit.
- 40acres 8y agoI haven't used Ruby before but I'd have to guess that their Array implementation utilities table doubling, if this is the case operations like concat, union, and difference should be linear.
- deleted 8y ago[deleted]
- mmahemoff 8y agoIn most real world applications, the cost of doing an in-memory data manipulation like this is negligible compared to I/O, waiting for the database etc. It's already the wrong language for someone who wants to squeeze out every CPU cycle, and imo it's a Good Thing for a language upgrade to embrace its strengths ie syntax sugar in the case of Ruby, as long as the perf hit isn't out of control. I'd agree developers should pick the right data structure for the job and some APIs can obscure the cost to the point of misleading developers into woefully wrong decisions. I just don't think it's the case with array ops like union as it's easy enough to handle them efficiently. I too would be interested to see any benchmarks though.
- saagarjha 8y agoI might be more receptive to this if this was added to all collections, then, rather than just arrays. (Maybe it was? I don’t write Ruby, so I don’t know for sure. Maybe Ruby doesn’t have a generic “collection” protocol?)
- mmahemoff 8y agoInteresting point. Ruby has an Enumerable module, but these methods were added only to Array. Since the main native collections (Set/Hash/Array) all support them now, they could probably be added to Enumerable in the future, with class-specific optimizations if necessary.
- dragonwriter 8y ago> This seems a bit too much sugar to me, to the point where people will end up writing inefficient code because they will thing these methods are more performant than they really are. Real-world performance and asymptotic complexity aren't the same thing. > Usually, I like to see explicit conversions to sets to make the performance characteristics explicit. The performance problem with this approach (for certain problems) when you have input arrays and want an output array is that Array->Set and Set->Array conversions are not free, and even if this is better in asymptotic complexity, you aren't always operating at the extends where asymptotic complexity dominates, so you can end up with code that is both excessively verbose and unnecessarily nonperformant just to make asymptotic complexity more apparent.
- yxhuvud 8y agoTypically, they will be linear on the combined size of the arrays. - for example is implemented by putting all elements of the right side in a hash map and then iterating through the first one and deleting all that can be found in the hash.