Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

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

Discussion in 'General Discussion' started by Arowx, Oct 26, 2013.

  1. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    12,173
    Actually that specific bit of code might run faster in C#. It uses strings, and where C# (and .NET in general) have pretty nifty underlying string management systems, in C++ everything gets done by brute force. I'm also pretty rusty on the passing semantics in C++, you might be passing *copies* of the string there...
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,154
    How does this "add to the loading time of your game"? That is, in any significant way, that anyone would actually notice?

    How does C++ have any relevance? The language has nothing to do with how Unity works. If you're talking about adding a different physics engine on top, then C++ or C# makes no difference as far as "dragging in a totally different physics engine or GUI". Those are not something you "drag in".

    Edit: oops, didn't see angrypenguin's post, which said basically the same thing.

    --Eric
     
  3. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    7,140
    Equivalent C++ Unity code
    Code (csharp):
    1.  
    2.     void Update()
    3.     {
    4.  
    5.  
    6.         float h = Input.GetAxis(InputAxis.Horizontal) * scrollSpeed;
    7.         float v = Input.GetAxis(InputAxis.Vertical) * scrollSpeed;
    8.         float d = Input.GetAxis(InputAxis.Mouse_ScrollWheel) * zoomSpeed;
    9.  
    10.  
    11.         Vector3 mousePos = Input.mousePosition;
    12.  
    Good point, they really should be constants actually I don't know why Unity doesn't convert the Input text keys to constant id's they would only have to inject an Axis class or struct with the relevant public properties and it would be so much faster and be typesafe. :D

    Actually could the not have made Invoke and SendMessage actually take a function delegate to be typesafe and more performant?
     
  4. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,408
    Because, as angrypenguin said, "C# (and .NET in general) have pretty nifty underlying string management systems."

    If you know the type and have a reference to an instance of that type, you can just call the method you need directly. Invoke and SendMessage make it possible to use duck-typing. SendMessage can even broadcast messaged.
     
  5. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,832
    And? This is a problem because?

    Source?
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    12,173
    Except it's not really equivalent at all, and implies a lot of changes (and a significant loss of flexibility) outside of the piece of code you're showing.[/quote]

    Once more, why do we care about the speed of something that happens maybe 8 times per frame, which is already reasonably fast anyway, as opposed to something that happens thousands of times per frame?

    And it already is typesafe. It uses a string index into a data structure, and you pass in a string. There's nothing about that which lacks type safety, and it's no more error prone than any other time you might use an object as an index into a data structure.
     
  7. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,205
    If you're actually trying to match Unity's class structure then the C++ example is incorrect anyway. It'd be Input::GetAxis(), not Input.GetAxis. And if InputAxis is supposed to be an enum, it'd just be "Horizontal" and "Vertical" rather than "InputAxis.Horizontal" or "InputAxis.Vertical" because enums aren't named scopes in C++.

    It'd probably have to be Input::get_mousePosition(), too, as C++ doesn't support properties.

    I encourage anyone who thinks C++ would be substantially faster to read the Mono source code. Go and figure exactly what additional work Mono is doing behind the scenes that would be 'skipped' if code was written directly in C++.