![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 ? 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | Re: LINQ - Speed penalty/anonymous types Thanks for your comment Marc. |
My System Specs![]() |
![]() |
| 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 | |||