Archive

Posts Tagged ‘GeometryGroup’

GeometryGroup.Bounds Performance

August 4, 2009 Mihai Leave a comment

GeometryGroup.Bounds Performace

GeometryGroup.Bounds Performace

I recently worked with GeometryGroup and I noticed the performance impact when using GeometryGroup.Bounds.

For simple geometries, everything seams to work smoothly, but when adding, let’s say … 100 000 geometries to the GeometryGroup, accessing its Bounds member takes a looong time … approx. 3 seconds (on my dev machine).

Needless to say, such a low performance makes the method unusable in most scenarios.

As a workaround for this issue, I found that using Rect class and its Union method to add the Bounds of each piece of Geometry works faster.

10 times faster.

Example:

//Execution time > 3 seconds

GeometryGroup group = new GeometryGroup();
Random random = new Random();
for (int i = 0; i < 100000; i++)
{
RectangleGeometry rectGeometry = new RectangleGeometry(new Rect(random.Next(), random.Next(), random.Next(), random.Next()));
group.Children.Add(rectGeometry);
}

Rect groupBounds = group.Bounds;

//Execution time < 0.3 seconds

GeometryGroup group = new GeometryGroup();
Random random = new Random();
Rect rectBounds= Rect.Empty;
for (int i = 0; i < 100000; i++)
{
RectangleGeometry rectGeometry = new RectangleGeometry(new Rect(random.Next(), random.Next(), random.Next(), random.Next()));
group.Children.Add(rectGeometry);
rectBounds.Union(rectGeometry.Bounds);
}

Rect groupBounds = rectBounds;