This week we continue with iterators to get the functionality of IEnumerable
without the nasty garbage creation. This week the little iterator library gets support for sorting and binary searching. Read on for the details!
Posts Tagged array
Back from a brief break, we pick up this week by finishing up the “modifying sequence operations” with some gems like RandomShuffle
and go through the “partitions” category with functions like Partition
and IsPartitioned
. These are all solid algorithms with a lot of potential uses, so read on to see how to use them with iterators and for the source code that implements them!
Continuing the series this week we’ll delve into the iterator functions that modify the sequence. This includes handy tools like Copy
, SwapRanges
, and Transform
. Of course this is all done without creating any garbage! Read on to see how and for the full source code.
Last week’s article introduced the concept of iterators as an alternative to the GC-heavy IEnumerable
. Today’s article expands the iterator library to include a bunch of more functions to make it useful. Think of these like the extension functions in System.Linq
: Any
, IndexOf
, etc. These have all been tailored to iterators and none of them will create any garbage whatsoever.
In C#, just about everything is an IEnumerable
. Since LINQ syntax, foreach
loops, and the System.Linq
namespace are all designed to work with IEnumerable
, you’ve got lots of tools to use. Unfortunately, the core of IEnumerable
is the GetEnumerator
function which usually creates garbage and eventually causes memory fragmentation and GC framerate spikes. Do we simply stop using all of these nice tools? Normally the answer is “yes”, but today’s article shows you another way.
While little utilized, C#’s struct
type can come in really handy sometimes. Today’s article shows how to use it to get a lot more mileage out of modern CPUs’ caches to really boost your app’s performance. Read on for some quick tips!
foreach
loops are really convenient, but are for
loops faster? It’s a simple question, but one that has really wide implications in almost any codebase. Today’s article tests them out to see which is faster for looping over arrays and List
s. Read on to see which is quicker!
Contrary to what you may have learned in a data structures class, linked lists are virtually always slower than just using arrays. The same goes for array wrapper classes like List
. Today’s article discusses why this is the case and tests it out with a C# Unity app to make sure that the real world validates the theory.
Last week’s article compared the performance of arrays with List<T>
and found List
lacking. This week we’ll optimize both List
and array to maximize performance regardless of which you choose to use.
System.Collections.List<T>
is used everywhere in C# code. Except for very special cases, it’s the replacement for arrays, linked lists, queues, and most other one-dimensional data structures. This is because it has all kinds of extra functionality, including the ability to grow in size on-demand. Today’s article wonders about how much performance is lost to gain this convenience and tests the List<T>
class against the lowly C# array: T[]
. How much performance are you giving up with List
and why is that happening? Read on to find out!