I have read a lot about the pros and cons of each , and i know delegates are usually for one listener, and notifications are for many. The question is about performance.
I have read this : NSNotificationCenter vs delegation( using protocols )?
I am sending audio signals from mic, to another class by notification . i know that here i should use the delegate BUT my question is : does delegates will be faster ? because i can see i have some frame rate issue(decreased), and i would like to know if the cause could be the using of notification instead of delegate, or there is no relation ?
Delegates, in much the same way, create a link between two objects, and you don't need to know what type the delegate will be, it simply has to implement the protocol. On the other hand, NSNotifications are like a radio station. They broadcast their message to whoever is willing to listen.
A notification dispatch mechanism that enables the broadcast of information to registered observers.
For those interested in performance I ran a simple test in swift using the XCTest framework's measureBlock
API. The short answer is that if calling in a loop, the difference will be significant.
Here is the code used to test:
public protocol MyTestClassDelegate: class {
func myTestDelegateCallback()
}
public let TestClassValueChangedNotification = "TestClassNotification"
public class MyViewModel {
public weak var delegate: MyTestClassDelegate?
public init() { }
public func doNotification() {
NSNotificationCenter.defaultCenter().postNotificationName(TestClassValueChangedNotification, object: nil)
}
public func doDelegation(value: Int) {
delegate?.myTestClassDelegateCallback()
}
}
And the Test Cases:
func testPerformanceNotifiction() {
measureBlock { () -> Void in
let testClass = MyTestClass()
for i in 0...100000 {
testClass.doNotification(i)
}
}
}
func testPerformanceDelegation() {
measureBlock { () -> Void in
let testClass = MyTestClass()
testClass.delegate = self
for i in 0...100000 {
testClass.doDelegation(i)
}
}
}
Results:
- Delegation:- - - - - - 0.957 seconds
- Notification Center: - 3.882 seconds
Other considerations are that the performance of NSNotificationCenter obviously may vary based on how many listeners there are for a given event, and the performance of the code executed by those listeners. Its also worth noting that NSNotificationCenter calls the notification listeners synchronously, and on the same thread on which postNotification
was invoked, which can be a gotcha when first approaching NSNotificationCenter.
If you find yourself in a scenario, (as I have) where you need one to many communication, and high performance, you might consider simply implementing an array of delegates. But you need not bother, because the performance of this is actually the worst option.
public func doMultipleDelegatation() {
for i in 0..<delegates.count {
delegates[i].myTestDelegateCallback()
})
}
func testPerformanceMultipleDelegation() {
measureBlock { () -> Void in
let testClass = MyTestClass()
testClass.delegates = [self]
for i in 0...100000 {
testClass.doMultipleDelegation(i)
}
}
}
Final Results:
- Delegation:- - - - - - 0.957 seconds
- Notification Center: - 3.882 seconds
- Multiple Delegation: - 6.488 seconds
Delegates come with less overhead and will therefore be executed much faster.
However, in general you should look on performance topics only there where they are likely to be an issue at all. For once-off tasks like sending a notification vs calling a delegate this should never be an issue. But when you plan to perform these in a loop with a variable (depending on data) number of intarations or for a number of data objects where you have fetched or received the data an cannot predict how many there will be - those are the situations where I would consider performance optimization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With