Archive

Archive for the ‘C#’ Category

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;

C# 3.0 Design Patterns – mistakes in examples

June 10, 2009 Mihai Leave a comment

I recently decided to refresh and extend my knowledge about design patterns.

I chose to read  C# 3.0 Design Patterns by Judith Bishop mostly because of the promise of having every example in C# 3.0.

C# 3.0 Design Patterns

C# 3.0 Design Patterns

It is actually a good book, with comprehensive examples and UML diagrams of 23 design patterns.

Because of the busy schedule, I barely have time for reading, but in the morning, when I go to work, the 30 minutes bus ride seems the perfect time for reading.

This morning I was very happy because I found a mistake in one of the examples. Not really something obvious or something I could bet I’m right (I had to double check it by running the example), but I was able to notice some really important bug in the code and the expected output. I would even say, a priority 0 bug, given the fact that the whole example was trying to prove some behavior in a wrong way.

Don’t get me wrong, I’m not criticizing, I’m just pointing out, that even at this level, mistakes can be done, and I’m able to spot them :)

Categories: Advanced, Books, C#, Programming