The following come simply draws a set of strings on the screen.
Performance with WPF is terrible.
It takes about one second for the screen to refresh for a 1000 * 1000 window - resize the window to verify.
When I use Windows Forms, it is much faster
Am I missing something
Is WPF much slower but since computer are getting faster, I am not supposed to be concerned
WPF and Windows Forms code :
WPF:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Globalization;
namespace TestPerformance
{
class App
{
[STAThread]
static void Main()
{
Application app = new Application();
Window w = new Window();
w.Content = new MyControl();
app.Run(w);
}
}
class MyControl : Control
{
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
Size s = this.RenderSize;
FormattedText txt = new FormattedText("wow", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), 12, Brushes.Black);
for (double d = 0; d < s.Width - 50; d += 20)
{
for (double h = 0; h < s.Height - 20; h += 20)
{
drawingContext.DrawText(txt, new Point(d, h));
}
}
}
}
}
Windows Forms:
class MyForm .....
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
Size s = this.Size;
Font font = new Font("Arial",12);
Brush brush = Brushes.Black;
for (double d = 0; d < s.Width - 50; d += 20)
{
for (double h = 0; h < s.Height - 20; h += 20)
{
e.Graphics.DrawString("wow", font, brush, new Point((int)d, (int)h));
}
}
}

Performance problem
ericrtodd
Your problem is that you're drawing ~2000 - 3000 formatted texts per OnRender(). The problem isn't the cost of rendering the text, but of the overhead of this many unique FormattedTexts that are all being rerendered per-frame.
Here's some alternative code I just whipped up that produces virtually identical output but draws 30-40 formatted texts per OnRender(). Give this a try and let me know if it solves your problem.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Globalization;
namespace TestPerformance
{
class App
{
[STAThread]
static void Main()
{
Application app = new Application();
Window w = new Window();
w.Content = new MyControl();
app.Run(w);
}
}
class MyControl : Control
{
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
Size s = this.RenderSize;
string st = "wow";
string aggregate = "";
double d = 0;
while (d < (s.Width - 50))
{
aggregate += st;
d += 24;
}
FormattedText txt = new FormattedText(aggregate, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), 12, Brushes.Black);
int count = 0;
for (double h = 0; h < s.Height - 20; h += 20)
{
drawingContext.DrawText(txt, new Point(0, h));
++count;
}
Console.WriteLine(count);
}
}
}
David
akaricky1
Thank you very much for your reply.
I'd like to display a (potentially very large) grid of data. Ideally, scrolling would be very smooth.
I tried virtualization. Virtualization works fine but not great. It takes a fraction of a second to add the new elements and remove the old ones. While not atrocious, scrolling is not smooth.
Which is why I tried going down to the visual layer. However, maybe it's not a good idea to begin with. Please advise.
By the way, I noticed that WPF is able to scroll large documents very smoothly. Would it be possible to accomplish the same thing with FrameworkEleemnt descendants
Matthew Trunnell
Hi Tom,
Thanks for letting us know about the performance trouble your having. Could you provide me on more details about what your trying to accomplish It feels like your trying to use WPF as if it were an immediate-mode API (which GDI+ is), and so there's probably a better way of telling WPF what you want to do. In the meantime, I'm going to profile this sample and see where time is being spent (I always recommend folks to profile their own apps too, see http://blogs.msdn.com/timothyc/ for details).
Thanks,
Tim Cahill [MSFT]
Software Design Engineer
Windows Presentation Foundation Performance
SteBirk