Tag Archives: WinRT WPF Performance

WinRT vs WPF Drawing Performance

Although WPF/XAML are great for developing user interfaces it does have a reputation for being slow at drawing. Certainly it is recommended you keep the visual tree down to less than 10,000 objects if you want anything like a responsive user experience.

So how does the new WinRT compare to WPF? There is no need to wonder when we can create a couple of simple tests and see how it works in practice. My test consists of a single XAML page that contains a TextBlock and Grid. The TextBlock will be used to indicate how many frames per second are being drawn and the Grid used to host instances of the Rectangle shape class. The WPF version of the XAML is as follows.

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <TextBlock x:Name="speed"/>
            <Grid x:Name="g" Grid.Row="1"/>
        </Grid>
    </Window>

The code behind is a little more verbose but simple enough. It creates rows and columns for the Grid and fills each cell with a Rectangle instance. It then kicks off a timer that fires as quickly as possible. The timer tick will update the Brush instances used to color the background and border of each Rectangle. By counting how many times per second the timer ticks we can see the update rate.

    public partial class MainWindow : Window
    {
        public int _count = 0;
        public Random _rand = new Random();
        public DateTime _startTime;
        public DispatcherTimer _timer = new DispatcherTimer();
        public List _shapes = new List();
        public List _brushes = new List();

        public MainWindow()
        {
            InitializeComponent();

            int bounds = 40;
            for (int i = 0; i &lt; bounds; i++)
            {
                g.RowDefinitions.Add(new RowDefinition() 
                    { Height = new GridLength(1, GridUnitType.Star) });

                g.ColumnDefinitions.Add(new ColumnDefinition 
                    { Width = new GridLength(1, GridUnitType.Star) });
            }

            for (int x = 0; x &lt; bounds; x++)
            {
                for (int y = 0; y  1000)
            {
                if (_count &gt; 0)
                    speed.Text = _count.ToString();
                else
                    speed.Text = "0";

                _count = 0;
                _startTime = DateTime.Now;
            }

            int index = _rand.Next(_brushes.Count);
            foreach (Rectangle tb in _shapes)
            {
                tb.Fill = _brushes[index++ % _brushes.Count];
                tb.Stroke = _brushes[index++ % _brushes.Count];
            }

            _count++;
        }
    }

Running on a Windows 7 machine we getting the following output…

WPF Rectangle Drawing

…which averages around the 15 frames per-second. It gives the same frame rate when run maximized or restored. Running the same test as a Metro app at full screen we get the following…

WinRT Rectangle Drawing

…which is 40 frames compared to the 15 for WPF. A nice 260% performance improvement. But applications are made up of more than simple rectangles. So with a minor change we can fill each cell with a TextBlock that has a new Text value set for each timer tick. Running this under WPF we get…

WPF Text Drawing Performance

…around 11 frames per second and the Metro app version comes out at…

WinRT Text Drawing Performance

…around 10 frames on average, the screenshot happened to be taken when it was at 9 but it averaged 10. So it seems that Text drawing performance is about the same and rectangle drawing is significantly faster under WinRT. Obviously this is only a rough and ready comparison but it is very positive to see that even at the Consumer Preview stage the WinRT implementation is at least at good and in many areas better than WPF. You have to assume that it will only get faster in the future as developer time shifts from adding features to improving performance.