InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Performance Problems with Lambdas

Posted by Jonathan Allen on Jul 05, 2007

Sections
Development,
Architecture & Design
Topics
Performance & Scalability ,
.NET
Tags
Visual Basic.NET ,
C# ,
Functional Programming

In LINQ Cookbook, Recipe 5: Concatenating the selected strings from a CheckedListBox, Microsoft's Visual Basic team demonstrates two ways to build string concatenators using features in VB 9.

In their first version, they show how to use LINQ's Aggregate syntax. The code that generates the comma separated list looks like this:

MsgBox( _
Aggregate Box In CheckedListBox1.CheckedItems _
Into Concat())

Concat itself is a an extension method implemented as such:

Public Module AggregateModule
Public Function Concat(Of Type)( _
ByVal ie As IEnumerable(Of Type)) As String
Dim str As String = ""
For Each item In ie
If str <> "" Then str &= ","
str &= item.ToString()
Next
Return str
End Function
End Module

Rather than going through the trouble of creating an extension method, both C# and VB developers also have the option of using Lambdas. The syntax is a little more complex, but not by much.

MsgBox((From c In _
CheckedListBox1.CheckedItems).Aggregate( _
Function(ByVal x, ByVal y) x + "," + y))

Bill McCarthy points out that this second syntax has some issues, including performance.

The extension method approach could be improved by using a string builder, but how do you use a String builder when using the lambda approach? If you can't then at the end of the day, the lambda ends up creating multiple strings for each concat, and in .NET that's one of the no-no's for writing string concatenation. The extension method though you can refactor and tune to use a StringBuilder.

Another issue Bill McCarthy brings up is that lambdas are not factored for reuse. Lambdas usually exist in the context of a single function, so unless they are returned as a delegate there is no way to share them. This is not as much as an issue for VB, which only has single-line lambdas in this release, but will be a concern for those using the longer lambdas possible in C#.

 

 

 

No comments

Watch Thread Reply

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.