Posts Tagged performance

Loop Performance: Part 4

Today’s article takes a break from the iterator series to investigate an interesting anomaly with the List.ForEach function: it’s surprisingly fast! So fast that it’s actually competitive with regular old for, foreach, and while functions. How can it be so fast when it has to call a delegate that you pass it for every single loop iteration? Read on for to find out!

Read the rest of this entry »

Tags: , , , , ,

No Comments

Enumerables Without the Garbage: Part 3

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.

Read the rest of this entry »

Tags: , , , , , , ,

No Comments

Enumerables Without the Garbage: Part 2

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.

Read the rest of this entry »

Tags: , , , , , , ,

No Comments

Enumerables Without the Garbage: Part 1

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.

Read the rest of this entry »

Tags: , , , , , ,

No Comments

Even More Ways Structs Create Garbage

Last time we saw that calling a non-default constructor on a generic struct (MyStruct<T>) causes garbage creation. That garbage creation is subtle, but can have big impacts on framerate and memory usage. Today we’ll see two more ways that structs can create garbage and hopefully avoid some pitfalls. Read on to find out how!

Read the rest of this entry »

Tags: , , ,

2 Comments

Loop Performance: For vs. Foreach vs. While

Today’s article expands on the previous loop test article to find out which kind of loop is truly fastest. Read on to find out!

Read the rest of this entry »

Tags: , , ,

3 Comments

If-Else is Really Expensive

Sometimes a tiny amount of code costs a huge amount of performance. This is especially true of built-in language features, which many programmers assume to be extremely cheap if not free. Today we’ll look at if and see just how much performance it can cost your app. Read on to see!

Read the rest of this entry »

Tags: , , ,

5 Comments

Better CPU Caching with Structs

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!

Read the rest of this entry »

Tags: , , , ,

2 Comments

FastList: A Solution to List’s GC Problems?

As we know, foreach loops create garbage when used with a List<T>. This happens the first time you iterate over one and it happens every time thereafter. A comment on that article shared a link to a class called FastList that was written expressly to solve the GC issue. Does it? How does its performance compare to plain old List? Today’s article puts it to the test to find out!

Read the rest of this entry »

Tags: ,

5 Comments

How Often Should You Force Garbage Collection?

One typical piece of advice for dealing with the slowness of Unity’s garbage collector is to periodically force a garbage collection, such as every 30 frames. Even Unity advises this. The idea is that you’ll spread out the garbage collection work across many frames rather than having a big spike that causes your frame rate to stutter. But the question remains- what’s the best rate to force the GC? Today’s article tries out various intervals to see which is best. Read on for the results!

Read the rest of this entry »

Tags: , ,

No Comments