Performance of Lambda Expressions

  • Thread starter Thread starter TWischmeier
  • Start date Start date
T

TWischmeier

Greetings,

we recently switched our target framework for our mobile application
to .netcf 3.0. This opened up the possibility of using lambda
expressions. I asked google about the performance impact of using
lambdas, but I could not find a suitable answer. I am talking about
the language construct itself, not about the difference between linq
and SqlCe-Queries, to be clear. A quick example, to disable all
Buttons in a Sequence:

Without lambdas:
IEnumerator<Button> btnEnum = buttons.GetEnumerator();
while(btnEnum.MoveNext()) {
btnEnum.Current.Disable;
btnEnum.Current.Visible = false;
}


With lambdas:
buttons.Select<Button>( b => {
b.Disable;
b.Visible = false;
});


How would those two codesnippets compare in regards to memory usage,
runtime, etc?

Best Regards,
Tim
 
Honestly the best way to know is to test them in your environment. We can
give you theory all day, but use case differences can make the compiler do
different things with small, apparently innocuous changes. Also, why are
you worry about perf at this point? Is perf a specific problem when
disabling buttons using either method? If not, use the one that makes the
code most readable.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
In the project I'm working on (CF 3.5 on WinMo 5.0/6.0) we've been
working with LINQ to collections and lambda expressions in general
quite extensively and so far I've never seen this approach make any
significant difference in terms of performance. If you get into a
performance issue it will be much more likely caused by something else
(like the db engine or UI creation).

As Chris said - it's worth to give up on a more readable construct
only if you have good evidence that it's indeed a bottleneck.

Regards,
Michał
 
* drozd wrote, On 16-9-2009 12:16:
In the project I'm working on (CF 3.5 on WinMo 5.0/6.0) we've been
working with LINQ to collections and lambda expressions in general
quite extensively and so far I've never seen this approach make any
significant difference in terms of performance. If you get into a
performance issue it will be much more likely caused by something else
(like the db engine or UI creation).

As Chris said - it's worth to give up on a more readable construct
only if you have good evidence that it's indeed a bottleneck.

Regards,
Michał

Do be careful if you are using a lot of anonymous types and var local
variables. These can grow your assembly in size pretty quickly. Which
makes it a lot slower to load (all executable code is verified and
hashes are being checked, so a larger file loads slower) if they are
included directly in your main .exe file.

The impact is lessened to slower load times due to the fact that the
runtime needs to load more bytes when these statements are in external
..dll assemblies.

See

http://themechanicalbride.blogspot.com/2008/10/silverlight-and-anonymous-types.html

For a background story (even though it's about silverlight, the same
applies to the CF).
 
Back
Top