SgDotNet
Singapore Professional .NET User Group -For Cool Developers

System.Windows.Shape performance

rated by 0 users
This post has 1 Reply | 1 Follower

Top 150 Contributor
Posts 14
pp Posted: 01-16-2008 2:36 PM

Hi, I'm trying  to understand 2D drawing performance with WPF, comparing to that of GDI+.  I wrote a simple applciation which simply draw 10000 rectangle and ellipse in the canvas. And it turns out that drawing in WPF is much slower than in GDI+.  I'm not sure whether this result is normal/expected, or this is not the right scenario to use WPF, or the way I code is wrong. Can someone help? Thanks.

private void cmdDraw_Click(object sender, RoutedEventArgs e)
{
    this.canvas1.Children.Clear();   
    DateTime dtStart = DateTime.Now;
    int max = 20;
    try
    {
        max = int.Parse(this.txtInput.Text.Trim());
    }
    catch { max = 20; }

    int i, posx, posy;
    posx =  0;
    posy = 20;
    double WMax = this.canvas1.ActualWidth - 20;
   
    //double HMax = this.canvas1.ActualHeight - this.canvas1.Margin.Bottom - 20;
    double right, bottom;
   
    for (i = 0; i < max; i++)
    {
        if (posx > WMax - 20)
        {
            posx = 0;
            posy += 15;
        }
               
        Rectangle rect = new Rectangle();
        rect.Name = "Rect" + i.ToString();
        rect.Fill = Brushes.Crimson;
        rect.Stroke = Brushes.Black;
       
        Thickness tk = new Thickness(posx, posy,0, 0);
        rect.Margin = tk;
        rect.Width = 15;
        rect.Height = 10;
        this.canvas1.Children.Add(rect);
       
        posx += 20;               

    }

    posx = 0;
    posy += 25;
    for (i = 0; i < max; i++)
    {
        if (posx > WMax - 20)
        {
            posx = 0;
            posy += 15;
        }


        Ellipse eps = new Ellipse();
        eps.Width = 15;
        eps.Height = 10;
        Thickness tk = new Thickness(posx, posy, 0, 0);
        eps.Margin = tk;
        eps.Fill = Brushes.LightSalmon;
        eps.Stroke = Brushes.Blue;
        eps.Name = "Eps" + i.ToString();
        this.canvas1.Children.Add(eps);
        posx += 20;
       
    }
    DateTime dtEnd = DateTime.Now;
    string sMsg = string.Format("Draw,{0},{1},{2},{3}",
        max, dtStart.ToString("MM/dd/yyyy HH:mm:ss.fff"),
        dtEnd.ToString("MM/dd/yyyy HH:mm:ss.fff"),
        ((TimeSpan)dtEnd.Subtract(dtStart)).TotalMilliseconds.ToString());
    this.listBox1.Items.Add(sMsg);

}

Top 500 Contributor
Posts 4

Hi,

Just saw your post about WPF Performance issues. I would like to give down few point about WPF here.

 WPF uses "Painter's Algorithm" to draw the screen and this is a very simple algorithm. But this algorithm performed poorly on the hardware which was available at that time so different algorithms were invented to support the hardware and GDI uses one of those algorithm. So why Microsoft used Painter's Algorithm is because with todays graphics hardware this model performs relatively fast.

So I feel this is the reason your WPF program performance is not as expected because I don't find any flaws in your code. I would also like to know what is the graphics hardware your using If it supports Pixel Shader 2.0 then you should not get this performance problem. WPF also renders with Anti-aliasing so these are the constraints towards the performance.

Another point is if you want to measure the performance of a .Net application you an use the Stopwatch class which is available from .Net 2.0.

 Simply do this

Stopwatch sw = Stopwatch.StartNew();

// Code to measure performance

sw.Stop();

Console.WriteLine(sw.Elapsed);

 

Suren. 

 

Page 1 of 1 (2 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems