Performance differences in Unity C# versus standalone apps

First you need to make sure you’re testing using a Release build for both environments.

How did you run your Visual Studio .exe? Because by default the .exes produced by VS run under .NET/Framework, while Unity’s .exe runs under Mono. So, yes you’ll find significant difference with Unity, since Unity is still using the Mono Runtime (an open source and cross-platform implementation of the old closed source .NET Framework), which is known to be slower than the old Microsoft’s own implementation (.NET Framework). And boy have they optimized .NET to be super performant since then, just check this huge page for performance improvements in .NET 8:

And that’s only .NET 8. Unity is missing on 6 other major versions (and 3 minor ones)

Now that Microsoft open sourced .NET Framework and made it cross-platform (and dropped the suffix “Framework”), Unity is finally ditching Mono in favor of the new shiny .NET 8 (or 9). You can follow the progress here: Unity Future .NET Development Status

Btw, in .NET 5+, there’s virtually no difference in a for vs foreach loop for arrays, and in .NET 8 for other collection types.

1 Like