Posts Tagged performance

Efficiently Keeping Lists Sorted

List<T> (and SafeList) have a great feature for fast lookups: BinarySearch. However, the list needs to be sorted in order to use it. You could call Sort() first, but that would give back all the performance you got with BinarySearch. It’s better to just keep the list sorted all the time. Unfortunately, there is no function on IList<T>, List<T>, or SafeList to efficiently insert an item into a list that’s already sorted. Today’s article presents an extension function that adds this functionality on to IList<T> and even the non-generic IList so your list will always be sorted for quick lookups with BinarySearch. Read on for the code, unit tests, and a performance test showing the advantages you stand to gain.

Read the rest of this entry »

Tags: , , , , ,

2 Comments

Delegates As Function Pointers: Performance Boost?

C# delegates can be used like function pointers. Assign it once and you don’t have to use an if over and over. But is the overhead of the delegate worth it? Today’s article puts it to the test to see if this a valid performance boost versus just using an if over and over. Read on to see if a delegate is worth your time.

Read the rest of this entry »

Tags: , , ,

4 Comments

For vs. Foreach

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 Lists. Read on to see which is quicker!

Read the rest of this entry »

Tags: , , , , ,

9 Comments

DateTime Performance

Unity’s Time class is an easy way to get the relative time. You can find the time since the app started with Time.time or the time between frames with Time.deltaTime. But what if you want to know the absolute time? You may need to display a clock to the user, send a timestamp over a network, or record when a game was saved. This is where System.DateTime comes in. It’s powerful and offers so much functionality that it’s natural to worry about about how slow it’ll be. So today’s article puts it to the test to find out how much time is being spent in operations like DateTime.Now which gets the current date and time. Is it quick enough that you shouldn’t worry? Read on to find out.

Read the rest of this entry »

Tags: , ,

No Comments

StrangeIoC Reflection Performance

StrangeIoC is a library that can help you build your Unity app with a “pure code” approach. Today’s article addresses one common concern with using StrangeIoC- it uses a lot of reflection. As we know, that’s really slow in Unity. StrangeIoC tries to work around it by letting you control when the reflection takes place so you can put it on a loading screen or some other convenient place. Today’s article finds out just how slow the reflection is to determine if this is really a valid reason to not use StrangeIoC (or other dependency injection frameworks). Read on to find out!

Read the rest of this entry »

Tags: , , ,

No Comments

Linked Lists Are Slow

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.

Read the rest of this entry »

Tags: , , ,

2 Comments

Optimizing Arrays and Lists

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.

Read the rest of this entry »

Tags: , , , ,

1 Comment

Array vs. List Performance

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!

Read the rest of this entry »

Tags: , ,

6 Comments

Virtual Function Performance

One type of function was left out of Unity Function Performance: virtual functions. Functions in C# are non-virtual by default and you have to explicitly use the virtual and override keywords to override them. Why not make this the default, like in AS3 or Java? Are virtual functions that much slower? Today’s article finds out! Should you be worried every time you make a function virtual?

Read the rest of this entry »

Tags: , , , ,

1 Comment

String.Format() vs. Concatenation vs. String Builder

What’s the fastest way to build a string in C#? We have several options available to us. string.Format() is a function built right in to the string class., Concatenation ("a" + "b") is a feature of the language itself! The System.Text.StringBuilder class is a built in class with a name that makes it sound like it’s purpose-built for building strings. Today I pit these three against each other to find out just which one you should be using to build strings as quickly as possible.

Read the rest of this entry »

Tags: , , , ,

9 Comments