guard a.isSubset(of: b) else { return false }

This should be the most efficient way to do what you’re trying to do.

> Using Swift 3 I have a function that's called extremely frequently and is 
> appearing regularly in Profiler runs. The function takes two 
> Set<String>instances and simply attempts to determine whether all items from 
> Set A are present in Set B (it's important to note that Set B may well 
> contain additional items).
> 
> I've attempted to approach the problem in two ways:
> 
> let diff = a.subtracting(b)
> guard diff.count == 0 else { return false }
> 
> 
> And also by simply iterating over the contents of Set A, like so:
> 
> for item in a {
> if !b.contains(item) {
> return false
> }
> }
> 
> 
> Both ultimately end up spending the majority of their time in 
> String._compareDeterministicUnicodeCollaton(String) ->Int. Which makes sense, 
> given what I'm doing - but ideally I'd like to come up with a more efficient 
> way of performing the above check. Swift's String representation is 
> incredibly robust, but for my needs the strings could be adequately 
> represented in ASCII encoding. I've also considered storing and comparing the 
> hashValue of the strings, to speed up comparisons...
> 
> Hopefully this is an acceptable question for this mailing list. I'm aware 
> that this may not be a Swift-specific question and could well be solved with 
> a more efficient data structure or approach, but I'd really appreciate some 
> input from more experienced Swift developers 
> :-)_______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
> 
> 
> 
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to