Performance question

I made some kind of prototype of draggble rectangles over surface (2d). One - using UIElement inherited objects, other using inheritance from Visuals. And very strange thing happens. Here is results: Creation (1000 elements) - UIElement/Visual = 1/100 Translations - UIElement/Visual = 100/1 Other words, it's takes much longer to create Visuals, but translate (move/drag/change) them much faster. What's the sense behind it? As far as I undertsand UIElement derrive from Visuals, but it's much faster to create UIElements then the same Visuals... Please advice
[564 byte] By [TamirKhason] at [2007-12-24]
# 1
Micro benchmarks are very tricky to write given the way JIT compilers work. I suggest you post your code for creating the elements and see if that clarifies things. I would not expect object creation time to be all that different between the two kinds of superclasses. I would expect that UIElements require more time to render and consume more memory.
MichaelLatta at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

>>I would expect that UIElements require more time to render and consume more memory.

Me too ;)

Follow the code

public class MyHost : FrameworkElement

{

SmartMadeHost smh;

OtherHomeMadeHost omh;

public enum HostType

{

Smart,

Basic

}

public static FrameworkElement DoHost(HostType hostType, int iterations, out TimeSpan benchmark, out double total2)

{

FrameworkElement elem;

DateTime start = DateTime.Now;

if (hostType == HostType.Basic)

{

elem = new OtherHomeMadeHost(iterations);

total2 = ((OtherHomeMadeHost)elem).GetTotalRenderTime;

}

else

{

SmartMadeHost smh = new SmartMadeHost(iterations);

elem = smh.GetContent;

total2 = ((SmartMadeHost)smh).GetTotalRenderTime;

}

DateTime end = DateTime.Now;

benchmark = end - start;

return elem;

}

}

public class SmartMadeHost : FrameworkElement

{

DragCanvas canvas;

double total;

public SmartMadeHost(int iMax)

{

canvas = new DragCanvas();

total = 0;

for (int x = 0; x < iMax; x += 20)

{

for (int y = 0; y < iMax; y += 20)

{

SmartRectangle elem = new SmartRectangle(x, y);

canvas.Children.Add(elem);

total += elem.GetTotal;

}

}

}

public Canvas GetContent

{

get { return this.canvas; }

}

public double GetTotalRenderTime

{

get { return total; }

}

class SmartRectangle : FrameworkElement

{

Point initialOffset;

Brush brushBackground;

double total;

public Brush Background

{

get { return brushBackground; }

set { brushBackground = value; }

}

public SmartRectangle(int x, int y)

{

total = 0;

initialOffset = new Point(x, y);

}

public double GetTotal

{

get { return total; }

}

protected override void OnRender(DrawingContext drawingContext)

{

DateTime dt = DateTime.Now;

base.OnRender(drawingContext);

Rect r = new Rect(initialOffset, new Size(20, 20));

drawingContext.DrawRectangle(Brushes.LightBlue, new Pen(Brushes.Red, 1),

r);

double t = ((TimeSpan)(DateTime.Now - dt)).TotalMilliseconds;

total += t;

drawingContext.DrawText(new FormattedText(

t.ToString("0.0"),

System.Globalization.CultureInfo.CurrentCulture,

FlowDirection.LeftToRight,

new Typeface(new FontFamily(), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),

10, Brushes.Black), initialOffset);

}

}

}

public class OtherHomeMadeHost : FrameworkElement

{

VisualCollection children;

DrawingVisual draggingRect;

Point initialOffset;

TranslateTransform initialTransform;

double total;

public OtherHomeMadeHost(int iMax)

{

// int iMax = 1000;

children = new VisualCollection(this);

total = 0;

for (int x = 0; x < iMax; x += 20)

{

for (int y = 0; y < iMax; y += 20)

{

DrawingVisual dv = CreateDrawingVisualRectangle(x, y);

dv.Transform = new TranslateTransform(0, 0);

children.Add(dv);

}

}

this.MouseLeftButtonDown += new MouseButtonEventHandler(MyVisualHost_MouseLeftButtonDown);

this.MouseLeftButtonUp += new MouseButtonEventHandler(MyVisualHost_MouseLeftButtonUp);

this.MouseMove += new MouseEventHandler(MyVisualHost_MouseMove);

}

public double GetTotalRenderTime

{

get { return total; }

}

void MyVisualHost_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{

initialOffset = e.GetPosition(this);

HitTestResult result = VisualTreeHelper.HitTest(this, initialOffset);

if (result != null)

{

draggingRect = result.VisualHit as DrawingVisual;

if (draggingRect != null)

{

draggingRect.Opacity = 0.4;

initialTransform = (draggingRect.Transform as TranslateTransform).Clone();

initialTransform.Freeze();

children.Remove(draggingRect);

children.Add(draggingRect);

Mouse.Capture(this);

}

}

}

void MyVisualHost_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

{

if (draggingRect != null)

{

draggingRect.Opacity = 1.0;

draggingRect = null;

Mouse.Capture(null);

}

}

void MyVisualHost_MouseMove(object sender, MouseEventArgs e)

{

if (draggingRect == null) { return; }

Point currentOffset = e.GetPosition(this);

(draggingRect.Transform as TranslateTransform).X = (currentOffset - initialOffset).X + initialTransform.X;

(draggingRect.Transform as TranslateTransform).Y = (currentOffset - initialOffset).Y + initialTransform.Y;

}

private DrawingVisual CreateDrawingVisualRectangle(int x, int y)

{

DateTime dt = DateTime.Now;

DrawingVisual dv = new DrawingVisual();

DrawingContext dc = dv.RenderOpen();

Point pt = new Point(x, y);

Rect r = new Rect(pt, new Size(20, 20));

dc.DrawRectangle(Brushes.LightBlue, new Pen(Brushes.Red, 1), r);

double t = ((TimeSpan)(DateTime.Now - dt)).TotalMilliseconds;

total += t;

dc.DrawText(new FormattedText(

t.ToString("0.0"),

System.Globalization.CultureInfo.CurrentCulture,

FlowDirection.LeftToRight,

new Typeface(new FontFamily(), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),

10, Brushes.Black), pt);

dc.Close();

return dv;

}

protected override int VisualChildrenCount

{

get { return children.Count; }

}

protected override Visual GetVisualChild(int index)

{

if (index < 0 || index > children.Count)

{

throw new ArgumentOutOfRangeException();

}

return children[index];

}

}

TamirKhason at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified