views:

881

answers:

2

I am having performance issues when rendering/rotating WPF triangles

If I had a WPF triangle being displayed and it will be rotated to some degree around a centrepoint, I can do it one of two ways:

  1. Programatically determine the points and their offset in the backend, use XAML to simply place them on the canvas where they belong, it would look like this:

    <Path Stroke="Black">
       <Path.Data>
          <PathGeometry>
             <PathFigure StartPoint ="{Binding CalculatedPointA, Mode=OneWay}">
                <LineSegment Point="{Binding CalculatedPointB, Mode=OneWay}" />
                <LineSegment Point="{Binding CalculatedPointC, Mode=OneWay}" />
                <LineSegment Point="{Binding CalculatedPointA, Mode=OneWay}" />
             </PathFigure>
          </PathGeometry>
       </Path.Data>
    </Path>
    
  2. Generate the 'same' triangle every time, and then use a RenderTransform (Rotate) to put it where it belongs. In this case, the rotation calculations are being obfuscated, because I don't have any access to how they are being done.

    <Path Stroke="Black">
       <Path.Data>
          <PathGeometry>
             <PathFigure StartPoint ="{Binding TriPointA, Mode=OneWay}">
                <LineSegment Point="{Binding TriPointB, Mode=OneWay}" />
                <LineSegment Point="{Binding TriPointC, Mode=OneWay}" />
                <LineSegment Point="{Binding TriPointA, Mode=OneWay}" />
             </PathFigure>
          </PathGeometry>
       </Path.Data>
       <Path.RenderTransform>
          <RotateTransform CenterX="{Binding Centre.X, Mode=OneWay}" 
                           CenterY="{Binding Centre.Y, Mode=OneWay}"
                           Angle="{Binding Orientation, Mode=OneWay}" />
       </Path.RenderTransform>
    </Path>
    

My question is which one is faster?

I know I should test it myself but how do I measure the render time of objects with such granularity. I would need to be able to time how long the actual rendering time is for the form, but since I'm not the one that's kicking off the redraw, I don't know how to capture the start time.

A: 

Have you tried using either the Perforator or the Visual Profiler included in the WPF performance tools? From the documentation it looks like these tools should be able to get you the information you're looking for. You'd have to build your application the first way, test it with these tools, take note of the performance information, and then do it all over again with the application built the second way to compare.

Derek
A: 

You could override the form's OnPaint method like this (pseudo code):

private void OnPaint(...)
{
    // Start timer

    base.OnPaint(...);

    // Stop timer
 }

or am I being incredibly naive?

ChrisF