Unlike C ++, there is no need to indicate that the method is virtual in Swift. The compiler will develop the following:
Performance indicators, of course, depend on the equipment.
- Embed Method: 0 ns
- Static Dispatch: <1.1ns
- Virtual dispatch 1.1ns (for example, Java, C # or C ++ at assignment).
- 4.9ns dynamic dispatch (e.g. Objective-C).
Objective-C, of course, always uses the latter. 4.9ns overhead is usually not a problem, as this will represent a small fraction of the total execution time of the method. However, when necessary, developers can easily return to C or C ++ where necessary. This is still an option in Swift, however, the compiler will analyze which of the fastest can be used, and try to make a decision on your behalf.
One of the side effects of this is that some of the powerful features provided by dynamic dispatch may not be available, as, as previously assumed, this was the case for any Objective-C method. Dynamic dispatch is used to intercept a method, which in turn is used:
- Cocoa property watchers.
- Instrumentation device model CoreData.
- Aspect Oriented Programming
With the latest version of Swift, even if the object is marked as "@objc" or extends NSObject, the compiler may still not necessarily use dynamic dispatch. There is a dynamic attribute that can be added to the method for failure.
source share