!
Today’s article is a follow-up toan article about AS3 from 2014. AS3 had a lot of issues related to performance, so it’s good to validate with another language to make sure it wasn’t some weird quirk related to Flash. In this case, we’ll port that test to C# and see if we can replicate the results.
To check it out, let’s run two very similar loops. Both of them just compute a simple sum, but one of them has an
if . Here’s how those loops look:
// No "if" int sum = 0; for (var i = 0; i < REPS; ++i) { int cur = i & 1; sum += cur; } // Includes an "if" sum = 0; for (int i = 0; i < REPS; ++i) { int cur = i & 1; if (cur > 0) { sum += cur; } else { sum += cur; } }
Notice that both the
if and
else cases do the same work: adding to the sum. So the result will be the same for both loops.
Now let’s check the timing for those loops with a very large
REPS value to see how long they take. Here’s the full script:
using UnityEngine; public class TestScript : MonoBehaviour { private string report; void Start() { const int REPS = 1000000000; var sw = System.Diagnostics.Stopwatch.StartNew(); int sum = 0; for (var i = 0; i < REPS; ++i) { int cur = i & 1; sum += cur; } var noBranchTime = sw.ElapsedMilliseconds; sw.Reset(); sw.Start(); sum = 0; for (int i = 0; i < REPS; ++i) { int cur = i & 1; if (cur > 0) { sum += cur; } else { sum += cur; } } var branchTime = sw.ElapsedMilliseconds; report = "Operation,Time\n" + "No Branch," + noBranchTime + "\n" + "Branch," + branch.3
- Unity 5.3.4f1, Mac OS X Standalone, x86_64, non-development
- 640×480, Fastest, Windowed
And here are the results I got:
The version with the
if-else block took 25% longer than the version without one. Again, both versions result in the same sum, so the
if-else block isn’t adding anything to the output.
There are multiple potential reasons for this performance discrepancy. First, there is an actual cost to do the
if : comparisons and jumps. Second, modern CPUs attempt to predict which instructions will be executed and the presence of an
if means that it’ll be wrong sometimes. In the case of this test, it’s wrong half the time and that can diminish the performance of the version with
if .
The simple takeaway from this test is to remember that two simple letters—
if —can carry a huge performance cost. Avoid branching with
if-else or
switch in performance-critical code if you can manage it. Let me know inif you’ve found any performance boosts like this, such as by removing an
if !