Posts Tagged performance

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: , , ,

4 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

Error Handling Performance

Having just concluded the series on handling errors without using exceptions, now’s a good time to check up on an assertion I made in the first part: exceptions are slow. A good question to ask is “compared to what?” So let’s put them up against good old error codes and the new Either type I presented in the series. Which is fastest when there is no error? Which is fastest when there is an error? Read on to find out!

Read the rest of this entry »

Tags: , , ,

No Comments

Event Performance: C# vs. UnityEvent

Unity programmers have their choice of two kinds of events. We could use the built-in C# event keyword or Unity’s UnityEvent classes. Which is faster? Which one creates more garbage? Today’s article finds out!

Read the rest of this entry »

Tags: , ,

9 Comments

Delegate() vs. Delegate.Invoke()

In reading others’ C# code I consistently see some programmers call delegates like a function—del()—and others use the Invoke method of the Delegate class: del.Invoke(). Is there any difference between the two? Is one better than the other? Today’s article finds out!

Read the rest of this entry »

Tags: , ,

No Comments

Using Structs to Avoid Creating Garbage

It’s easy to forget about struct in C#. After all, it’s not available in other languages like Java or AS3 and it seems to have fewer features than good old class. But struct can really help you out when it comes to garbage creation! Today’s article discusses some strategies to get the most out of struct. Read on to learn how to use structs to put a stop to that pesky garbage collector!

Read the rest of this entry »

Tags: , , , ,

No Comments

Do Foreach Loops Create Garbage?

We know that we should reduce the garbage our code produces to lighten the load on Unity’s garbage collector. The trouble is that many of the ways we’re creating garbage are hidden from us. One such way to inadvertently create a lot of garbage is to use a foreach loop… at least that’s what we’ve been told. Do foreach loops really create garbage for all types of arrays, lists, dictionaries, and the rest of the collections? Do they create garbage for every loop or just the first one? Today’s article investigates to put these questions to rest. Are you safe using foreach loops or should you re-write everything to use for. Read on to find out!

Read the rest of this entry »

Tags: , , , , , ,

4 Comments

Too Many Coroutines: A Queue Solution

Unity’s coroutine support is great. So great that it’s easy to go overboard and end up with too many of them. That could be for any number of reasons. Perhaps the coroutines are using too much memory or have too many files open at once. In any case, you’ll need to find a way to limit how many are running at a single time. Today’s article introduces a solution to the problem that queues coroutines so you never have too many running. Read on to learn about the solution and for the class that implements it.

Read the rest of this entry »

Tags: , , ,

2 Comments