I needed the TakeLast<T>(int n)-style LINQ function . I encountered this message the StackOverflow: qaru.site/questions/30043 / ... . I liked this answer simply because it was a simple implementation. Then another colleague of mine indicated that it Reverse()should be more expensive than that Skip(length - n). It made me write a test.
TakeLast<T>(int n)
Reverse()
Skip(length - n)
Here are the competing features.
public static IEnumerable<T> TakeLast<T>( this IEnumerable<T> c, int n ) { return c.Reverse().Take( n ).Reverse(); } public static IEnumerable<T> TakeLast2<T>( this IEnumerable<T> c, int n ) { var count = c.Count(); return c.Skip( count - n ); }
I have timed the execution of getting the last 10 elements of an enumeration Enumerable.Range( 0, 100000 ). I found that:
Enumerable.Range( 0, 100000 )
TakeLast()
.NET Sciddle ( , .): http://dotnetfiddle.net/ru7PZE
TakeLast2()
, . LINQ , , . Count . Count , . , , , . , , , .
Count
, , , , JIT . - , ( ), .
, ( ICollection). , -, , . IList, , - say IQueryable, , , , . , , .
ICollection
IList
IQueryable
Source: https://habr.com/ru/post/1535382/More articles:Substring from start to next comma Report Builder - substringhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1535378/vb6-form-with-a-comctl32ocx-listview-leads-to-application-crash-on-windows-8&usg=ALkJrhgoUpnsJvjhyT231qrJszQOnkQwxwCross-compiling node.js modules on mips Big Endian - node.jsEmacs setq before downloading - emacsTitanium PushNotification How to display in one group? - androidHow to read a data table from a SQL Server stored procedure - c #how to check null value in datarow string in c # - c #https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1535385/eclipse-sts-fails-to-start-after-update-due-to-exception-loading-system-bundle&usg=ALkJrhhNIMqEvjl4U0L2ut9yDF7Ok7MlxACreating a POST request using X-Accel-Redirect with Rails? - ruby-on-railsWhat is KnownType in WCF - c #All Articles