Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - LINQ - Speed penalty/anonymous types

Reply
 
Old 03-31-2008   #1 (permalink)
Peter Larsen [CPH]


 
 

LINQ - Speed penalty/anonymous types

Hi,

I'm wondering if the use of anonymous types slow down the app at runtime or
not.

See the following sample:

var obj1 = from item in list select new { item.ClassID, AnyName
= item.ClassID };
foreach (var item in obj1)
{
string s1 = item.ClassID;
string s2 = item.AnyName;
}

I know lot of the work is done at compile time, but i still wondering if it
slows down the application to use anonymous types:
Is it slower to use ?

BR
Peter



My System SpecsSystem Spec
Old 03-31-2008   #2 (permalink)
Jon Skeet [C# MVP]


 
 

Re: LINQ - Speed penalty/anonymous types

Peter Larsen [CPH] <PeterLarsen@xxxxxx> wrote:
Quote:

> I'm wondering if the use of anonymous types slow down the app at runtime or
> not.
>
> See the following sample:
>
> var obj1 = from item in list select new { item.ClassID, AnyName
> = item.ClassID };
> foreach (var item in obj1)
> {
> string s1 = item.ClassID;
> string s2 = item.AnyName;
> }
>
> I know lot of the work is done at compile time, but i still wondering if it
> slows down the application to use anonymous types:
> Is it slower to use ?
No. It's anonymous at compile time, but it's a perfectly ordinary type
as far as the CLR is concerned. All you're doing in the above is
calling a constructor and then retrieving properties.

Of course, in the above case you could just go directly to the members
of the list, but I'm assuming in real life that you actually have a
reason to use the anonymous type in the first place.

--
Jon Skeet - <skeet@xxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
My System SpecsSystem Spec
Old 03-31-2008   #3 (permalink)
Peter Larsen [CPH]


 
 

Re: LINQ - Speed penalty/anonymous types

Thanks for your comment.
It's just sample code - i'm not using it anywhere.

/Peter
Quote:

>
> No. It's anonymous at compile time, but it's a perfectly ordinary type
> as far as the CLR is concerned. All you're doing in the above is
> calling a constructor and then retrieving properties.
>
> Of course, in the above case you could just go directly to the members
> of the list, but I'm assuming in real life that you actually have a
> reason to use the anonymous type in the first place.
>
> --
> Jon Skeet - <skeet@xxxxxx>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk

My System SpecsSystem Spec
Old 03-31-2008   #4 (permalink)
Marc Gravell


 
 

Re: LINQ - Speed penalty/anonymous types

In this case, with a list - it would be *slightly* more efficient to just
access each item in the list, since it avoids creating a short-lived object
per row - i.e.

foreach(var originalItem in list) {
// do something with originalItem.ClassID etc
}

However, if "list" is actually an IQueryable (or similar) source such as an
LINQ-to-SQL DataContext, then your original (projection-based) code would be
more efficient: in this case, the provider can usually do something clever -
i.e. with SQL it can build a SELECT statement that only includes the two
columns, and doesn't worry about change-tracking etc.

Marc


My System SpecsSystem Spec
Old 04-01-2008   #5 (permalink)
Peter Larsen [CPH]


 
 

Re: LINQ - Speed penalty/anonymous types

Thanks for your comment Marc.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Tweaker anonymous? General Discussion
Performance penalty between dynamic- and fixed-size virtual hard disks? Virtual PC
Aero Performance Penalty Enormous Vista performance & maintenance
Performance penalty with DEP? (Data Execution Prevention) Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46