Better Benchmarking with Additive and Multiplicative Baselines

In my recent blog posts I've been benchmarking different ways to cast in C#. If you have a keen eye, you may have noticed that the results do not match up. In one experiment, a direct cast (no cast at all) takes 15ns, while in another experiment, a more complicated generic cast

Read More

Covariant and Contravariant Casting is 3x Slower than Dynamic

Previously, we saw that covariant and contravariant casting is slow: 100x slower than normal casting. It turns out that covariant and contravariant casting is even slower than casting to dynamic and using dynamic dispatch: 3x slower than dynamic. This is significant as IEnumerable<T>

Read More

Casting to IEnumerable<T> is Two Orders of Magnitude Slower

Casting to generic interfaces that use covariance or contravariance is two orders of magnitude slower than normal casts in C#. This means casting to IEnumerable<T> is two orders of magnitude slower too. This result was quite unexpected and very surprising.

In this post, I investigate the cost of casting to implementations, interfaces, generic interfaces, covariant …

Read More

C# 7: Micro-Benchmarking the Three Ways to Cast Safely

As we saw in my previous post, there are three ways to cast safely in C# 7. In this post, I micro-benchmark the three methods of safe casting and dive into the IL to understand the differences.

The three methods of safe casting (from my previous post

Read More