How much faster would Unity games/apps be if it used C++?

If Unity provided it’s engine as a DLL with header files so users could write their games in C++ instead of C# how much faster would they run?

They’d run as fast as a piece of string is long.

depends on the programmer?

Or twices as long as half it’s length.

Fact C# and Mono is SLOOOWER than Compiled C++, but the question is how much faster is C++, 2x, 5x the only real way to find out is to ask and hope the good developers at UT who have worked on a C++ version of Unity (Due to the Apple App Store Native apps only policy issue).

This would not make the Unity game engine run any faster, but it would increase the speed of the game logic we write which could greatly improve frame rates, allowing for more stuff in our 16ms (60fps) game logic window and therefore better games.

If you’re developing in C# then syntactically your code could very easily be converted to C++ (*).

  • yes there would be some gotchas with collections, pointers and references but nothing a programmer could not work through and even automate into a code conversion program. :stuck_out_tongue:

But on modern systems, does it really matter? There have been a whole slew of tests and studies comparing C++ to C#. The thing about those tests, they don’t all agree on which is faster. Especially on the tests that are actually meaningful, they can come out tied more often than not. Granted, I think most of them are between .Net and native, so there is that. (If I remember which ones help illustrate my point specifically, I shall link to them…but I has 5 minutes before I need to head out)

Don’t get me wrong, a) I don’t want this to turn into another argument on the matter, and b) I LOVE pointers. Love them. I had a recent problem, parameterizing a sweep across a 3D* array that would allow me to sweep in any of the six directions with the same code, wherein the perfect solution was to use pointers…but, I couldn’t use unsafe code for this solution, so I had to come up with a bastardized exploit to get around it.

*It was a 3D array represented in a 1D array in the structure of (x + y * width + z * width * height).

You already can do most of your game logic in C++ though; I’ve done it with libraries and module interfaces for editor access.

As an indie game developer working on VR enabled games (it’s the future, this time, honest ;)) that need to hit or exceed 60fps or include a vomit bag.

That gives me < 16ms to run a game world (ideally, according to John Carmack, we should be running at 120fps so < 8ms), anything that takes up a chunk of this time means the game has to be reduced in some way, so in a way using C# Mono is a limiting factor on what the Unity game engine can deliver.

My understanding is that Mono is now owned by another company and this prevents Unity from progressing to a newer versions of Mono with improved GC, optimisations and features.

Could now be a good time to take Unity to C++, Unreal Engine 4 has already made this move?

But isn’t this is an indirect linkage, your dll is being called from C# which is being called from the Unity Engine, you gain performance and speed but add another layer of communication and overhead?

Optimization? Occlusion culling? Aren’t there ways to bring any game to the minimum fps by trading off view distance and reducing overdraw?

I shipped a game with over 2.5mil polygons and it used the kinect for input and was able to get it running perfectly (had it automated, running 24+ hours without issue). It was on a micro computer 7"x7"x4" form factor with integrated graphics…

Also worked on an oculus rift + kinect project on a team of 12 that had a badass scene with several thousand draw calls before optimization.

its depends on programmer AND TIME - so sometimes c# can be faster than c++ because c# is easier a lot than c++ if we need special optimising (threading? etc.) so c# has faster workflow and you can spend saved time to more optimise in c# (unsafe code?)

but if performance is only once criteria then c++ is the best option - c++ + assembler rulz ^^

Yes, but let’s say that you do all of the in engine optimisations and get the game running at about 30fps and looking great, your using 33ms so your still over budget by 18ms. How much of that overhead is being used by Mono and C#?

Also I like to use procedural/dynamic worlds which do not work well with Unity’s current static baked occlusion culling system.

If we had a better mono implementation it’d probably help a lot more than all the testing and redocumentation that would be required for adding an entirely new language and workflow.

If you’re this hardcore C++ you might wanna migrate to Ogre3D.

Not much if any speed difference considering most games are GPU bound on most platforms. In cases where you ARE actually CPU bound then yes it can help you a little with this version of mono. MS’s compiler is gives considerably closer performance to C++ and for inexperienced programmers may even be slightly faster.

That could really help with performance overall, but your still using Mono which is generally slower than .Net which is slower than C++.

It is a chunk of work, but in theory Unity could provide the engine as a dll with header files for the C++ programmers to build their games, the interfaces could be very similar to those provided already in C# and API documentation can be automatically generated from comments in the code by the programmers.

Unity already uses external editors for code it would just mean that C++ projects would work more like the Unity XCode projects for iOS/Mac with an extra step in the build/run process.

As mentioned earlier, “slower” is an incredibly difficult to quantify term in these cases. Adding C++ support might not necessarily provide any realistic performance increase since most of what’s happening in Unity is handled at lower levels by the engine itself.

If you are only talking about the scripting side, probably not much.

If it gave us more access to the internals of the engine, ie physx, you could completely change up the engine for your needs (or mess it up. ha)

For simpler games, yes, but if you take an RTS style game e.g. Starcraft or a Simulation style game e.g. Civ with layers of systems dealing with things like pathfinding, strategic AI, unit behaviours and dynamic systems and lots of active units then C#'s overhead is a limiting factor.

Most benchmarks indicate that a C++ program takes a fraction of the time to run of a similar C# program how much of a fraction varies between programs.

Games written in C++ perform better than C# quite simply because you are batch processing the JIT compilation so it does not need to be done at all, in addition to this C++ compilers can see the whole of the project and optimise accordingly JIT compilers are limited in their optimisation scope.

Yes there will be cases where C# can go toe to toe with C++ but C++ did not have to do any JIT compilation so it was already in the lead.

OK let’s put it another way, what is the most complex game you have seen developed with Unity, by complex I mean big open world games with very high unit counts and or complex layers of simulation like Civ?

Are there any RTS’s, MMO’s, Sims made with Unity that just blow you away with the number of enemies active at any one time?

So Unity becomes a modular framework and all the module providers have to do is provide appropriate interfaces. But couldn’t that leave Unity open to being a frontend for the Unreal or Crytech engines?