NET APIs and also provides indexers that take an NSObject, an NSString or a C# string as well as an enumerator. The NSDictionary type implements the ICollection, IDictionary and the generic ICollection and IDictionary interfaces, which makes it convenient to use with existing. Var dict = NSDictionary.FromObjectsAndKeys (values, keys) Public class NSDictionary : Foundation.NSObject, CoreData.INSFetchRequestResult, Foundation.INSMutableCopying, Foundation.INSSecureCoding, IDisposable, >,, >, type NSDictionary = class You will get better results with the Swift arrays if you mutate them in place: for i in 0.Dictionary that provides mapping from keys to values. However the buffer duplication due to copy-on-write happens at every iteration. The nonlinear growth you see is due to the fact that the array buffer increases don't occur that often, as the array implementations have a pretty good heuristic for the capacity increase, in order to avoid allocations every time a new element is added. And the bigger the array, the more it takes to duplicate the buffer. And as soon as you mutate that local array, the copy-on-write mechanism triggers a copy of the array buffer, which results into another heap allocation. The problem with your Swift array code, as Alexander pointed out in his comment, is the fact that you create a local copy of the array that is stored into the dictionary. And the new buffer allocation logic should be fairly similar in both Swift and Objective-C. Note that both Swift array and NSMutableArray under the hood kinda do the same things: when they have no more allocated space for new items, they allocate a new buffer. $ swiftc MutableDictionary2.swift -o MutableDictionarySwift2 -framework Foundation -O Print("Time spent: \(Date().timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate)") Swift with NSMutableArray: import Foundation 17:41:17.686 MutableDictionaryObjc Number of elements: 7 $ clang MutableDictionary.m -o MutableDictionaryObjc -framework Foundation -O3 timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate) I benchmarked this approach against Objective-C and Swift with NSMutableArrays, and with 1M entries it seems to be 180x slower, and seems to be exhibiting non-linear growth, suggesting that the compiler is not able to sufficiently optimize this, either. However, this seems to be very inefficient. Is there a way to put mutable arrays as values to Swift dictionaries without performance losses? When I was searching around, most approaches seemed to suggest grabbing the array from the dictionary as a var, adding to it, and then setting it back to the dictionary ( example). How to subscribe self on the event of Device Orientation(not interface orientation)?.Best way to update badgeValue of UITabBarController from a UIView.UINavigationBar Appears Under StatusBar.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |