Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelativeLayout vs nested Linear Layout performance

For a long time I heard that RelativeLayouts are slow. In a couple of talks I recall hearing that this type of layout calls onMeasure twice and that for some reason is a drag to performance.

However, I am taking a performance course at udacity and I watched the following video:

https://www.youtube.com/watch?time_continue=303&v=gK9tdeqqigE

Here, the instructor used the Hierarchy viewer tool to compare the rendering cost of the same viewgroup using a relative layout and nested linear layouts.

In the video, the relativelayout is the clear winner, which contradicts everything that I have heard until now about the issue.

Could you please help me to understand in which circumstances each approach is better?

Thank you

like image 588
Javier Ventajas Hernández Avatar asked Sep 18 '16 19:09

Javier Ventajas Hernández


1 Answers

That's a very broad question and there's no single, simple answer. LinearLayout is generally simpler (and therefore faster) than RelativeLayout, but LinearLayout has a problematic case if you nest multiple of them inside each other, with weights on the same axis. Then it has to iteratively divide up the space and this takes lots of layout passes (it's so bad there's a lint warning against this).

Even when you avoid that case, then with nested LinearLayouts you will still have a deeper view hierarchy compared to using RelativeLayout, so while LinearLayout is faster, that balances out at some point.

So it becomes the same thing as with all things performance: the only way to be absolutely sure is to measure and see what happens.

like image 99
Barend Avatar answered Oct 15 '22 09:10

Barend