I had been using nested views in one of the screens in my android app. Now that I have removed all of them and used a ConstraintLayout
for performance gains, I want to measure how much the performance has increased. Is there a way to measure the performance gain?

- 5
- 4
1 Answers
The easiest way is probably just to profile the app using a profiler using the old code and the new and see how much inclusive time is spent in onLayout in both versions.
Expect the difference to be less dramatic than you expect. The reality is your main thread is going to be idle most of the time, improving layout efficiency is really rarely a big factor in your app's performance. I would never refactor a nested design for that, I'd refactor a nested design if using constraint layout gave me cleaner XML for my view. Also remember that depending on what features you use, constraint view may not be faster. Its quicker to layout a nested linear layout than it is to calculate a whole bunch of constraints on multiple inner views, for example.

- 90,003
- 9
- 87
- 127
-
Thanks, @gabe-sechan. Do you think I should include the time spent in `onMeasure` as well? – Vignesh Jul 15 '21 at 11:05
-
@Vignesh the real problem with nested views is actually onDraw. Because its called far more often than the other two. If you are going to see a difference, its in total draw time. But really its unlikely on modern hardware you're going to see a big difference. A lot of the advice around that stuff was made when Android's drawing was a LOT less sophisticated and was mostly blocking calls to graphics memory. Which doesn't mean don't use ConstraintView- it can be great. It means refactor like that for code/design clarity, not for performance. – Gabe Sechan Jul 15 '21 at 14:58
-
Thanks @GabeSechan, you were right. After refactoring, I have lost performance. Anyway, I am not going to revert it as the loss is negligible (the screen renders like it did before, no visible delay/lag) and my code looks a lot cleaner. – Vignesh Jul 16 '21 at 04:55