[英]Struct with local references vs Class with local references? Swift performance
As far as I know if a struct contains more than one reference as property stored, it's (2*reference) more expensive to allocate it than a class. 据我所知,如果一个struct包含多个引用作为属性存储,它的(2 *引用)分配它比一个类更昂贵。 Is it the same case in local variables?
局部变量中的情况是否相同?
struct ViewNavigationRouter {
private let firstViewController : UIViewController
init(firstViewController: UIViewController) {
self.firstViewController = firstViewController
}
func navigate(from: UIViewController) {
let viewController1 : UIViewController = xxx
let viewController2 : UIViewController = xxx
let viewController3 : UIViewController = xxx
}
}
class ViewNavigationRouter {
private let firstViewController : UIViewController
init(firstViewController: UIViewController) {
self.firstViewController = firstViewController
}
func navigate(from: UIViewController) {
let viewController1 : UIViewController = xxx
let viewController2 : UIViewController = xxx
let viewController3 : UIViewController = xxx
}
}
Thanks in advance :) 提前致谢 :)
It seems, it is the same in this case as all the members in struct ViewNavigationRouter and in class ViewNavigationRouter are - instances of classes (firstViewController : UIViewController). 看起来,在这种情况下它是相同的,因为结构ViewNavigationRouter和类ViewNavigationRouter中的所有成员都是 - 类的实例(firstViewController:UIViewController)。 I do not see more wide context of this, but it looks like this here.
我没有看到更广泛的背景,但它看起来像这里。
I suggest you to read this https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html many times, specially focusing to parts Structures and Enumerations Are Value Types and Classes Are Reference Types. 我建议你多次阅读这个https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html ,特别关注零件结构和枚举是值类型和类是参考类型。
The most important difference is related to that what happends when you copy the content identifier is "pointing to". 最重要的区别与复制内容标识符时“指向”时发生的情况有关。 I suggest you further reading:
我建议你进一步阅读:
https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.htm and to tak a look to examples. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.htm并查看示例。 Than you will see that difference is not about memory performance, but on the way how ARC clears memory.
你会发现差异不是关于内存性能,而是关于ARC清除内存的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.