Grid and StackPanel, which has the better performance? Grid and StackPanel, which has the better performance? windows windows

Grid and StackPanel, which has the better performance?


You may be interested in the answers to this question: In what order are Panels the most efficient in terms of render time and performance?

The short answer is it depends on how many children the panels have, and how those elements are sized and positioned. But in most cases, a StackPanel will be more efficient than a Grid as it has both a faster measure and arrangement pass.

To quote from the accepted answer:

Grid

Defines a flexible grid area that consists of columns and rows.

This can be the most performance intensive panel if proportional sizing or auto sizing is used. Calculating child item size can be a complex combination of the native size of the item and the layout specified by the grid. Layout is also the most complicated of all the panels. Slow to medium performance for the measure pass and slow to medium performance for the arrangement pass.

StackPanel

Arranges child elements into a single line that can be oriented horizontally or vertically.

The StackPanel measures its children using either native or relative sizing in the opposite direction from its orientation and native sizing in the direction of its orientation (alignment does nothing in this direction). This makes it a mid-level performer in this area. The Arrangement pass is simply, just laying out the items in order. Probably the second-best performance for this pass. Medium performance for the measure pass and fast performance for the layout pass.

Also in regards to memory consumption, both objects are different and take up different amounts of memory, and a Grid has RowDefinitions and ColumnDefinitions, so it actually contains more objects than your StackPanel