Posts Tagged performance

Do Foreach Loops Still Create Garbage?

Tags: , , , ,

Over a year ago I wrote an article title Do Foreach Loops Create Garbage using Unity 5.2 and tested foreach with a variety of collections: List, Dictionary, arrays, etc. Since then Unity has a new C# compiler and version 5.6 has been released. Is it safe to use foreach now? Read on to find out!

Read the rest of this article »

6 Comments

Which Hash Set is Fastest?

Tags: , , ,

Say you need to keep track of things you’ve already done, perhaps to avoid doing them again. What’s the fastest way to do that? HashSet<T> seems like a natural fit, so you might choose that without a second thought. But is it faster than similar collections like Hashtable and Dictionary<TKey, TValue>? Today’s article puts all three to the test to see which one can insert elements, check for containment, and remove elements the quickest. Read on for the surprising results!

Read the rest of this article »

2 Comments

An Alternative to Events

Tags: , , ,

C# has built-in events and they work fine in Unity projects. End of story, right? Not so fast! Have you ever wondered why the Unity API doesn’t have any C# events in it? Or why Unity made their own UnityEvent class for UGUI? Maybe there are some valid reasons to avoid C#’s events. Today’s article discusses an alternative with some serious upsides. Read on to learn more!

Read the rest of this article »

8 Comments

Unity Function Performance Followup

Tags: , , , , , ,

By request, today’s article follows up on my Unity Function Performance article from a year and a half ago using Unity 5.0. It adds on GameObject.SendMessage and virtual functions to get a more complete picture of how various function calls in Unity perform. Of course it runs these tests using Unity 5.4 to see if there have been any changes in the engine. Read on for the results!

Read the rest of this article »

7 Comments

Should You Cache Array.Length and List.Count?

Tags: , , ,

Every time I see for (var i = 0; i < array.Length; ++i) I wonder if accessing that Length property is slow. Should I cache it? It’s comforting to know that for (int i = 0, len = array.Length; i < len; ++i) is only dealing with local variables except on the first loop. Local variables must be faster, right? Likewise, I wonder the same thing about List<T>.Count. I finally got around to running a test to see if caching these length properties makes any performance difference. The answers might surprise you!

Read the rest of this article »

4 Comments

Problem and Solution: The Terrible Inefficiency of FileStream and BinaryReader

Tags: , , , , ,

File I/O can be a major performance bottleneck for many apps. It’s all too easy to read files in a way that is massively inefficient. Classes like FileStream and BinaryReader make it really easy to write super slow code. Today’s article explores why this happens and what can be done about it. Read on to learn more!

Read the rest of this article »

6 Comments

Enumerables Without the Garbage: Part 7

Tags: , , , , , , ,

Today we’ll wrap up the iterator series by finishing up porting C++’s <algorithm> header. We end up with a library of functions for common LINQ-style algorithms but without any of the garbage creation that slows our games down. Read on for the source and examples!

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 6

Tags: , , , , , , ,

We’re nearing the end of the series to build a no-garbage replacement for System.Linq. Today we tackle functions that work on already-sorted ranges and functions that work on ranges that are in heap order. These include common set operations like “union” and “intersection”. Read on to see how to use them and for the updated library that you can use to eliminate your garbage creation!

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 5

Tags: , , , , , , ,

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!

Read the rest of this article »

2 Comments

Enumerables Without the Garbage: Part 4

Tags: , , , , , , ,

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!

Read the rest of this article »

No Comments