简体   繁体   中英

Measuring CPU usage rendering a UIView in Xcode

I have a view on a nib in Interface Builder. It's got a several UIStackViews within the nib, which I used because it's a easy to align UI elements. A colleague suggested I not use UIStackViews because they're "expensive" from a computational standpoint. The alternative is setting constraints for individual elements manually.

What Xcode tool can I use to validate my colleague's assertion and how would I use it? The best thing I've found is this , but I was hoping for something a little more granular. The nib in question is a UITableViewCell .

they're "expensive" from a computational standpoint

Okay, I will now attempt to demonstrate that that's utterly bogus.

A stack view is not magic. A stack view does not perform any special run-time adjustments. A stack view is just a constraint-maker, no more, no less. So are you. Everything a stack view does, you could do. And the stack view's resulting constraints are exactly the same constraints you would have created yourself (assuming you even know how). Therefore, the constraints themselves are not more expensive just because the stack view made them.

So let's talk about the initial generation of the constraints. Well, the stack view generates its constraints by rote. It's just a simple formulaic approach based on the parameters already given by such things as the stack view's settings and (in some cases) the intrinsic content sizes of the arranged subviews. So it takes effectively no time at all for the stack view to spit out the constraints that it generates, and it only has to do it once.

So if the generation of the constraints is not expensive, and if the constraints themselves are not expensive, where is the expense? It's nowhere.

Someone might argue that for a particular desired outcome using a stack view is unnecessarily complicated, or lazy, so it would have been "better" to make the constraints yourself; but "expensive" doesn't seem to me to be a genuine charge that can be laid against stack views.

In digging around, I found a repo called LayoutFrameworkBenchmark . I ran the benchmarks in iOS 12 on an iPhone 7+ simulator. I gathered the data and dumped it in a Google Sheet. It does indeed show UIStackView is pokey versus other layout methods.

I now see why there seem to be people in both camps, however in the real world, the human eye is unlikely to notice much of a difference unless it's a huge collection. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM