GeometryGroup.Bounds Performance

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;

One thought on “GeometryGroup.Bounds Performance

  1. SmediaLam

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

    Cheers
    Christian, iwspo.net

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s