Unity slower than visual studio C# ?

Hello everyone.

I am converting some parts of my previous games from actionscript to C#, and I go far better performances on visual studio than in Unity. So I made some test computing the Fibonacci sequence, and it seems that Unity performances drop a lot when making a lot of small memory allocation, while visual studio handles them a lot better.

Using the code bellow, I got :

  • fiboRecu(40) : 1045 ms in visual, 1497 ms in unity
  • fiboRecuClass(40) : 4091 ms in visual, 24775 ms in unity

Allocating a lot of classes explodes the execution time in unity, going from 43% slower to five times slower. And obviously, my library use a lot of small classes (with a lot of function calls too)

Is it something known ? Or is there settings to magically make things better ?

(If you are interested by numbers, there are more of them here)

public struct SResult
{
	public SResult (int i_result)
	{
		m_result = i_result;
	}
	public int result ()
	{
		return m_result;
	}
	int m_result;
}
public class CResult
{
	public CResult (int i_result)
	{
		m_result = i_result;
	}
	public int result ()
	{
		return m_result;
	}
	int m_result;
}

class CFiboPerf
{
	public static string checkPerf(int i_value)
	{
		string finalResult = "(" + i_value + ") ";
		Stopwatch timer = new Stopwatch();
		timer.Start();
		int result = fibo(i_value);
		timer.Stop();
		finalResult += "" + timer.ElapsedMilliseconds;
		timer.Start();
		int resultRecu = fiboRecu(i_value);
		timer.Stop();
		finalResult += ", R:" + timer.ElapsedMilliseconds;
		timer.Start();
		int resultStruct = fiboStruct(i_value).result();
		timer.Stop();
		finalResult += ", S:" + timer.ElapsedMilliseconds;
		timer.Start();
		int resultRecuStruct = fiboRecuStruct(i_value).result();
		timer.Stop();
		finalResult += ", RS:" + timer.ElapsedMilliseconds;
		timer.Start();
		int resultClass = fiboClass(i_value).result();
		timer.Stop();
		finalResult += ", C:" + timer.ElapsedMilliseconds;
		timer.Start();
		int resultRecuClass = fiboRecuClass(i_value).result();
		timer.Stop();
		finalResult += ", RC:" + timer.ElapsedMilliseconds;
		return finalResult;
	}
	static protected int fibo (int i_rank)
	{
		int final = 0;
		for (int repeat = 0; repeat <= 10000000; ++repeat)
		{
			int result = 1;
			int previous = 1;
			for (int iR = 1; iR < i_rank; ++iR)
			{
				result = result + previous;
				previous = result - previous;
			}
			final = result * ((repeat % 2 == 0) ? 1 : -1);
		}
		return final;
	}
	static protected int fiboRecu (int i_rank)
	{
		return (i_rank <= 1) ? 1 : (fiboRecu(i_rank - 1) + fiboRecu(i_rank - 2));
	}
	static protected SResult fiboStruct (int i_rank)
	{
		SResult final = new SResult(0);
		for (int repeat = 0; repeat <= 10000000; ++repeat)
		{
			int result = 1;
			int previous = 1;
			for (int iR = 1; iR < i_rank; ++iR)
			{
				result = result + previous;
				previous = result - previous;
			}
			final = new SResult(result * ((repeat % 2 == 0) ? 1 : -1));
		}
		return final;
	}
	static protected SResult fiboRecuStruct (int i_rank)
	{
		return (i_rank <= 1) ? new SResult(1) : new SResult(fiboRecuStruct(i_rank - 1).result() + fiboRecuStruct(i_rank - 2).result());
	}
	static protected CResult fiboClass (int i_rank)
	{
		CResult final = new CResult(0);
		for (int repeat = 0; repeat <= 10000000; ++repeat)
		{
			int result = 1;
			int previous = 1;
			for (int iR = 1; iR < i_rank; ++iR)
			{
				result = result + previous;
				previous = result - previous;
			}
			final = new CResult(result * ((repeat % 2 == 0) ? 1 : -1));
		}
		return final;
	}
	static protected CResult fiboRecuClass (int i_rank)
	{
		return (i_rank <= 1) ? new CResult(1) : new CResult(fiboRecuClass(i_rank - 1).result() + fiboRecuClass(i_rank - 2).result());
	}
}

The difference between Unity and VisualStudio is Mono and the overhead from Unity’s own garbage collector. Also you should check if you run both in .Net 3.5 because thats the newest .Net version Unity supports.