Thursday, September 25, 2008

[.NET] Generics Perfomance Myth (and Tony Northrup VS Wendy Sarret)

According to Microsoft, Generics are faster than casting (even in absence of boxing/unboxing).

Tony Northrup - co-author of MCTS 70-536: Application Development Foundation (which BTW totally sucks, a review will probably follow) - states in the same book the following:

I haven’t been able to reproduce the performance benefits of generics; however, according to Microsoft, generics are faster than using casting. In practice, casting proved to be several times faster than using a generic. However, you probably won’t notice performance differences in your applications. (My tests over 100,000 iterations took only a few seconds.) So you should still use generics because they are type-safe.


I haven't been able to reproduce such performance benefits myself (and I am, as a Certified .NET Butcher, way more reliable than MCTS books) - so I'd say the performance gain is "supposed" more than "significant", unless someone is able to prove it. Until that moment .NET Generic amazing performance will remain a myth.

If you know something more than me about this - please share.


P.S. BTW - isn't Tony Northrup real HOT (I'd rather date him over Wendy Sarret)?

kick it on DotNetKicks.com

9 comments:

kevin.gadd said...

I think the idea has always been that it's a 'potential' performance gain due to better information accessible to the compiler and JIT. I've never seen any credible statements suggesting that there are across-the-board performance gains to be made from using generics instead of casting, since the two apply in different areas anyway.

jmorrill said...

You should see a speed increase with generics + primitives (int, float, etc) because there is no boxing/unboxing involved. Boxing is only done on primitives because they need a managed pointer to wrap them.

Johnny Idol said...

Yep - the problem is the SHOULD there.

Ben said...

I'm confused how you don't see the performance increase.

Throwing a ton of Int32s into an ArrayList is bluntly slower then throwing the same amount into a List of Int32

Proof: Generics Makes a Great (and fast) Meal

Scuffia said...

The problem is not with ints...its clear from your proof that the List<int> has more performance in that case...the book anyway states correctly, like your proof, that List<MyClass> has less performances than an ArrayList, despite the assumption that with arraylist you have to do some explicit casting to use your custom objects (that is more CPU consumptive). As a matter of fact in the book we are not told about boxing/unboxing but rather on explicit casting...that is the ambiguity and the solution.

Ben said...

Ahh... I get what you are saying. If this book is telling you that ArrayList is less performant then generics because of explicit casting then it is clearly wrong. Whipping things back and forth between the stack and the heap (boxing/unboxing) is what kills performance. But with casting an object to an explicit class there is very minimal cost since all reference types are derived from object to begin with.

Johnny Idol said...

Yep - the argument is not about primitives. Microsoft states that Generics provide better performance than casting between reference types.

I tried the code in the link you posted - he's getting

Class Array: 42XX
Class List: 47XX

I am getting (with a better CPU)

Class Array: 26XX
Class List: 33XX

Now, either case (mine of his), I wouldn't call that "almost the same" - there's a rough 25% increase in the one that's using Generics on my test.

Tony said...

By the way, I'm releasing a new version of that book in November of 2008. It's 50-60% brand new (I re-wrote the other author's chapters), and all known errors have been corrected.

P.S. In yo face, Wendy Sarret.

Johnny Idol said...

@Tony

I kind of got the feeling the crap chaps weren't written by you since chaps with notes from you are quite good. The overall quality goes down a lot though.

Given the news, I should probably wait for the new book before facing the exam. I went through the whole book twice and I am finding extremely difficult to get decent results on test simulations.

Cheers