U# vs C#
U# is noticeably slower than regular C#, to benchmark it I decided to execute two scripts in U# and C#, one calculates the n-th element in the Fibonacci series, the second one generates a maze.
To make the tests a bit fairer, I disable the C# compiler optimization, since the U# compiler does not optimize your scripts
Recursive functions
For this test I executed a script that finds the nth element in the Fibonacci series, the Fibonacci method looks like thisĀ
[RecursiveMethod]
public int FibonacciRecursive(int n)
{
if (n <= 1)
return n;
else
return FibonacciRecursive(n - 1) + FibonacciRecursive(n - 2);
}
Since FibonacciRecursive
is a recursive function, the [RecursiveMethod]
attribute need to be added.
This is really a horrible way to calculate the nth element in the Fibonacci serie because certain method calls get called multiple times, and the number of method call rises very quicklyl :
Fibonacci(1)
calls the Fibonacci method 1 timeFibonacci(2)
calls the Fibonacci method 3 timesFibonacci(10)
calls the Fibonacci method 177 timesFibonacci(22)
calls the Fibonacci method 57313 time
So I felt like this test is an amazing way to determine how well U# performs this task, and I executed Fibonacci(22)
in C# and U#
C# time : 0.189ms
U# time : 684.577ms
U# was 3629 times slower!
Maze generator
In my "Circuit Master" world, I wrote a custom maze generator algorithm, which I unfortunately cannot share because the algorithm is about 1600 lines long.
I decided to compare the execution time of that algorithm because that would be a more realistic test, it uses pretty much everything (Unity functions, custom list implementations, for-loops, bit manipulations etc.).
The algorithm does not call any recursive functions, but implements a custom stack.
C# time : 1.609 ms
U# time : 972.712 ms
U# was 604 times slower!
Based on the previous results, we can see that U# is much slower than regular C#, so keeping the code optimized is even more important in U#!