ios - Do aspect ratio constraints reduce performance? -
tl;dr:
usually, horizontal constraint equations , vertical constraint equations independent of each other an aspect ratio constraint links both dimensions, merging 2 smaller sets of linear equations 1 big set. understanding 2 smaller sets should easier solve combined big set , thus, i expected aspect ratio constraints reduce performance. however, simple test 100 constrained views in each dimension showed no difference in performance. why?
the question in detail:
constraints linear equations
in auto layout, each layout constraint linear equation:
view2.attribute = multiplier * view1.attribute + constant
a non-ambiguous , non-conflicting layout given, when set of constraints has 1 solution.
inside method layoutsubviews()
system resolves constraints i.e. computes frames subviews these constraints. task solve system of linear equations done applying gauss algorithm.
x , y: 2 independent sets of equations
now long there no aspect ratio constraints involved the horizontal , vertical dimension independent of each other. thus, there 1 set of h linear equations horizontal constraints , 1 set of v linear equations vertical constraints. these can solved separately.
however, adding aspect ratio constraint view links both dimensions. instead of 2 independent sets of linear equations system has solve 1 bigger set of h + v linear equations.
the complexity of solving sets of linear equations
as complexity of solving system of n linear equations
somewhere between o(n2) , o(n3), depending on algorithm, must faster solve 2 systems h , v equations 1 system h + v equations. hence expect process of resolving constraints (i.e. layoutsubviews()
method) takes notably longer there @ least 1 aspect ratio constraint present.
to figure out created empty sample project, added 100 views along horizontal , 100 views along vertical axis , constrained them properly. measured time of layout process:
override func layoutsubviews() { let t1 = mach_absolute_time() super.layoutsubviews() let t2 = mach_absolute_time() print(t2 - t1) }
then replaced 1 of vertical constraints aspect ratio constraint , measured time again. the result pretty same. part don't understand.
why aspect ratio constraint not have bad impact on layout performance?
here's setup of views created , constrained. better visibility screenshots show 20 views in each direction instead of 100 views measured time.
i don't know implementation details logic argument seems fair.
the simple test describe basic able tell difference, computer / processor fast @ making calculations small sample size 100 views completion time, or rather variance between completion times minimal.
your test should use thousands or hundreds of thousands of views.
also, computers / processors lots of different things time, they're parallel processing. on multiple cores, each core multiple different things 'at same time. impacts results don't know happening while timing.
your test should run thousands or hundreds of thousands of times.
so, if increase 'complexity' of testing, , number of equations solved should begin see differences in relative cost solve equations.
Comments
Post a Comment