A peek past lambda

Rémi Forax forax at univ-mlv.fr
Sun Aug 21 07:37:28 PDT 2011

On 08/21/2011 02:11 PM, Llewellyn Falco wrote:
> I'm curious as to the following statement:
>> Lambdas are slow as hell in C#, I don't want to do the same mistake.
> check out http://diditwith.net/2006/10/05/PerformanceOfForeachVsListForEach.aspx
> it would appear they can be are faster than regular blocks of code ?!?
> ( this seems crazy, but I know I've seen this is java as well, where
> the "overhead" of a method call doesn't pan out as expected)

First all the tests are flawed in this benchmark,
a JIT can easily see that the sum is not used and discards the whole loop.

Second, iterating over a List using an Iterator is slower than using
an index, in C# as in Java (*).
Moreover, the .Net JIT recognizes List.forEach and generate a specific
assembler code for it (Scala uses the same trick at compile time).
That's why it's faster.

So this post is a flawed benchmarch on a special case where
we learn that if you special case something, it works well :)

I said that lambdas are slow in C# because I was involved
in a project aiming to develop a DSL with two backends.
One using the JVM the other using .Net.
This DSL had a very limited form of lambda and was not using
the C# library.
The performance of the .Net delegates was so awful that I had to
write a lot of compiler tricks to avoid to use delegates.

> I am wondering if perhaps the lambdas in C# are being confused with
> LINQ in C#. Linq can preform slowly, but that isn't because of the
> lambdas, it's because of the implementation of linq. A lot of times
> people are only using lambdas with linq and start to blur the lines.
> Alternatively, performance bottlenecks are also commonly misplaced
> when done without a profiler.

linq = C# extension method + lambda, both are slow.

> My question is, is there a performance difference between
> a();
> b();
> if b is a static method?
> is there a performance difference between
> a();
> otherObject.a();
> ??


> All of the above always struck me as very crazy fast, and if so why
> would you add complexity to make lambdas different? It would seem the
> only optimization lambdas would benefit from is the realization that
> some of them could be a singleton instead of a individual objects.
> just spitballing here,


>   but you could solve this one easily by
> replacing the " new LambdaAnonymousClass() " call with
> "LambdaAnonymousClass.Create()" and then in the creation of the
> LambdaAnonymousClass check if there is any Field methods to determine
> if it should be a singleton instance or not.

We can do better.

> of course this raises an interesting question of the equals()&
> hashCode() method, has that been discussed already?
> --
> Llewellyn Falco
> www.approvaltests.com


* With the JDK7 the performance of the loop with an Iterator are really 
   to the one with an index. So close that you don't have to think about 
that anymore.

More information about the lambda-dev mailing list