简体   繁体   English

宽高比限制会降低性能吗?

[英]Do aspect ratio constraints reduce performance?

tl;dr: TL;博士:

Usually, the horizontal constraint equations and the vertical constraint equations are independent of each other but an aspect ratio constraint links both dimensions , merging the two smaller sets of linear equations into one big set. 通常,水平约束方程和垂直约束方程彼此独立,但纵横比约束链接两个维度 ,将两个较小的线性方程组合并为一个大组。 From my understanding the two smaller sets should be easier to solve than the combined big set and thus, I expected aspect ratio constraints to reduce performance . 根据我的理解,两个较小的集合应该比组合的大集合更容易解决,因此, 我预期宽高比限制会降低性能 However, a simple test with 100 constrained views in each dimension showed no difference in performance. 但是,在每个维度中具有100个约束视图的简单测试显示性能没有差异。 Why? 为什么?


The Question in Detail: 问题详情:

Constraints are linear equations 约束是线性方程

In Auto Layout, each layout constraint is a linear equation: 在自动布局中,每个布局约束都是一个线性方程:

view2.attribute = multiplier * view1.attribute + constant

A non-ambiguous and non-conflicting layout is given, when the set of all constraints has exactly one solution. 当所有约束的集合恰好具有一个解决方案时,给出非模糊且非冲突的布局。

Inside the method layoutSubviews() the system resolves the constraints ie it computes the frames for all subviews from these constraints. 在方法layoutSubviews()内部,系统解析约束,即它根据这些约束计算所有子视图的帧。 The task is to solve a system of linear equations which could be done by applying the Gauss algorithm . 任务是解决线性方程组,这可以通过应用高斯算法来完成。

x and y: Two independent sets of equations x和y:两组独立的方程

Now as long as there are no aspect ratio constraints involved the horizontal and the vertical dimension are independent of each other . 现在只要不涉及纵横比约束,水平和垂直尺寸就彼此独立 Thus, there is one set of h linear equations for the horizontal constraints and one set of v linear equations for the vertical constraints. 因此,存在一组用于水平约束的h线性方程和用于垂直约束的一组v线性方程。 These can be solved separately. 这些可以单独解决。

However, adding an aspect ratio constraint to the view links both dimensions. 但是,向视图添加宽高比约束会链接两个维度。 Instead of two independent sets of linear equations the system has to solve one bigger set of h + v linear equations. 系统必须求解一组更大的h + v线性方程组,而不是两组独立的线性方程组。

The complexity of solving sets of linear equations 求解线性方程组的复杂性

As the complexity of solving a system of n linear equations is somewhere between O(n 2 ) and O(n 3 ), depending on the algorithm, it must be faster to solve two systems with h and v equations than one system with h + v equations. 由于求解n个线性方程组的复杂度介于O(n 2 )和O(n 3 )之间,取决于算法,使用hv方程求解两个系统必须比使用h +的一个系统更快v方程。 Hence I would expect that the process of resolving constraints (ie the layoutSubviews() method) takes notably longer as soon as there is at least one aspect ratio constraint present. 因此,一旦存在至少一个宽高比约束,我就会期望解决约束的过程(即layoutSubviews()方法)花费更长的时间。

To figure this out I created an empty sample project, added 100 views along the horizontal and 100 views along the vertical axis and constrained them properly. 为了弄清楚这一点,我创建了一个空的示例项目,沿水平方向添加了100个视图,沿垂直轴添加了100个视图并正确约束它们。 Then I measured the time of the layout process: 然后我测量了布局过程的时间:

override func layoutSubviews() {
    let t1 = mach_absolute_time()
    super.layoutSubviews()
    let t2 = mach_absolute_time()

    print(t2 - t1)
}

Then I replaced one of the vertical constraints with an aspect ratio constraint and measured the time again. 然后我用纵横比约束替换了一个垂直约束并再次测量时间。 The result was pretty much the same. 结果几乎相同。 This is the part that I don't understand. 这是我不理解的部分。

Why does an aspect ratio constraint not have a bad impact on layout performance? 为什么宽高比约束不会对布局性能产生不良影响?

Here's the setup of the views that I created and constrained. 这是我创建和约束的视图的设置。 For better visibility the screenshots only show 20 views in each direction instead of the 100 views for which I measured the time. 为了获得更好的可见性,屏幕截图仅显示每个方向的20个视图,而不是我测量时间的100个视图。

没有宽高比约束的视图的屏幕截图 具有宽高比约束的视图的屏幕截图

I don't know the implementation details but your logic argument seems fair. 我不知道实现细节,但你的逻辑论证似乎是公平的。

The simple test you describe is just too basic to be able to tell the difference, the computer / processor is so fast at making the calculations that for a small sample size like 100 views the completion time, or rather the variance between completion times is minimal. 你描述的简单测试太基本了,无法分辨出来,计算机/处理器在进行计算时如此之快,以便在100个小样本大小的情况下查看完成时间,或者说完成时间之间的差异很小。

Your test should really use thousands or hundreds of thousands of views. 您的测试应该真正使用数千或数十万个视图。

Also, computers / processors do lots of different things all the time, they're parallel processing. 此外,计算机/处理器一直在做很多不同的事情,它们是并行处理。 Sometimes this is over multiple cores, but each core also does multiple different things 'at the same time. 有时这是在多个核心上,但每个核心同时也会做多个不同的事情。 This impacts your results as you don't know what was happening while you were timing. 这会影响您的结果,因为您不知道在计时时发生了什么。

Your test should really be run thousands or hundreds of thousands of times. 您的测试应该真正运行数千或数十万次。

So, if you increase the 'complexity' of the testing, and the number of equations to be solved then you should begin to see the differences in the relative cost to solve those equations. 因此,如果你增加测试的“复杂性”和要解决的方程的数量,那么你应该开始看到解决这些方程的相对成本的差异。

Adding an aspect ratio constraint doesn't necessarily make the set of equations dependent on each other, with the constraints you described the equation for x position and width of all the views in the top row can be calculated without the necessity to solve first the vertical equations or joining the equations, once the engine solves the first set of equations it only needs to solve the y equations with a constant instead of a system of equations, and the same applies for the vertical column of views. 添加宽高比约束并不一定使得方程组彼此依赖,使用您描述的约束x方式的方程和顶行中所有视图的宽度可以计算而无需首先求解垂直方向方程式或连接方程式,一旦引擎解决了第一组方程式,它只需要用常数而不是方程组求解y方程,这同样适用于垂直视图列。

The engine that solves this equations (I guess) have heuristics that can improve performance based on the previous conclusions, and a lot more I think. 解决这个方程的引擎(我猜)具有启发式,可以根据之前的结论提高性能,我认为还有更多。

I think that you could force a situation which can only be solved by solving the system of equations, but I'm afraid that Interface Builder is going to show constraint errors, but I cannot think of such a situation now. 我认为你可以强迫一种只能通过求解方程组来解决的情况,但我担心Interface Builder会出现约束错误,但我现在想不到这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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