1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Are you an artist or level designer going to Unite Europe? Join our roundtables there to discuss artist features.
    Dismiss Notice
  6. Unity 5.6 is now released.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

newby performance question... which language is best

Discussion in 'Scripting' started by JonnyHilly, Sep 4, 2009.

  1. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    692
    So I just starteg going through all the tutorials.
    Seems lik all the examples are in j-script. but also can work in C# etc....

    Which is best for performance ?
    are all the examples in JS ? are there alternate C# examples ?

    thanks
    Jon
     
  2. Sam at FPS

    Sam at FPS

    Joined:
    Sep 1, 2009
    Posts:
    80
    It is all compiled into the same language (CIL) in the end so there is no difference in performance.

    But yes, the majority of examples are in JS.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,133
    The performance is the same. They all compile to the same CIL code.

    Edit: feh, if only I'd been a few seconds faster. ;)

    --Eric
     
  4. rozgo

    rozgo

    Joined:
    Feb 7, 2008
    Posts:
    158
    Something inside of me wants to say C# is better for performance. Maybe because C# is a more strictly-typed language and the compiler-to-bytecode can perform some (maybe insignificant) last minute optimizations. In theory, the CLI constructs should be the same, but I don't agree.

    I have no proof though. But the dynamic nature of Javascript may be more permissive than C#, allowing you to easily create low performance code. Maybe #pragma strict can force you into strict typing.

    Anyway, choose the one you know the most. Understanding how to get the most out of your language might be better than the language itself.
     
  5. NeilM0

    NeilM0

    Joined:
    Mar 31, 2009
    Posts:
    135
    I would lean towards C#. If you know JavaScript there are a number of things in Unity's "Javascript" that don't work the same. When you hit one of these situations the solution always seems to be... go with the C# way of doing things. (eg, reading the current date. JS returns 01/01/0000, but using C# methods in JavaScript gets you the actual date)

    However most of the samples are in JavaScript and if you know neither language, you might want to start out with that.
     
  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    Personally, I use JavaScript for everything. It's not really slowed me down or caused performance issues as of yet, and I found it much easier to learn than C# and Boo.

    To each their own, really.
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    there is no "C# methods in JavaScript".
    Its all .NET code and all must use the same .NET functionality to do their work.

    JS is just another syntax for the same thing (its not nor comparable to the javascript from website development above simple syntax similarity)
     
  8. fallingbrickwork

    fallingbrickwork

    Joined:
    Mar 16, 2009
    Posts:
    1,072
    I don't think there is any real performance gains between languages... but... there may be development speed gains between systems. I develop mainly in a Windows environment, so Visual Studio + ReSharper would steer me towards C# anyday (but that is solely a personal choice). There may be similar Mac or Javascript IDE's but i don't know of them.

    Matt.
     
  9. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    184
    Yeah, yeah, I'm necroposting, but I didn't really want to create a new thread for the same discussion, especially when this thread is linked to in a sticky.

    I just want to share some results that, whether or not it changes your view on which language is more efficient, may at least prove interesting in some way.

    Anyway, I was testing whether assignment was faster than checking so I could implement some overkill optimization, and decided to speedtest using both C# and UnityScript. Below are the codes that I used.

    For UnityScript:

    For C#:

    So after running those scripts separately in an empty scene (run one script first, then quit and run other script) about a handful of times each, I got the definitive answer I was looking for (assignment is faster than checking as far as Unity is concerned). But also interesting was that UnityScript took twice the amount of time to run the same code that C# ran.

    On average, 1 billion iterations of assignment took 1.77 seconds in UnityScript, while C# only needed 0.52 seconds, and 1 billion iterations of checking (accessing the variable + comparison) took 2.05 seconds in UnityScript, but only 1.03 seconds in C#.

    I was never convinced that UnityScript ran as fast as C#, but since my speedtest was never intended to compare UnityScript and C#, I was a little bit surprised that the results I got did exactly that for me.

    Having said that, 1 second divided by 1 billion is a very small number and is quite negligible for most applications, so you should continue coding in UnityScript if you prefer it over C#. Still though, the results I got are quite interesting.
     
  10. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    18,970
    try it again with #pragma strict at the top of the js example so it doesn't constantly cast. You need #pragma strict or js will be slower.

    I tested with #pragma strict and had identical or near identical results, with js being faster in some cases (probably due to noise).

    #pragma strict should be more widely documented and it prevents js from casting all the time and trying to guess what numbers you are using. Nice try/benchmark though even if your findings are wrong.
     
    Last edited: Jun 28, 2011
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,133
    Those aren't the results I get at all (click for larger version):

    $compareCvJS.png

    That's C# on the left, JS on the right. As you can see, not only are C# vs. JS virtually identical in speed (JS being a teensy bit faster, actually--this was consistent over several runs), but so is assignment vs. checking.

    That's not what #pragma strict does, at all. #pragma strict does nothing if your code is already compatible with the requirements for #pragma strict. I didn't add it when I ran those tests; I just copied pasted the scripts as-is. #pragma strict is purely a compile-time syntax-checking thing, and has no effect on the code.

    --Eric
     
    Last edited: Jun 28, 2011
  12. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    184
    That's odd. I doubt that my comp being 2 years old has anything to do with it. Could it be because I'm using Unity 3.1?
     
  13. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    As far as mixing the two, Its alot easier to assume C# to be able to use all features from a JS script where as JS doesnt support all of C#'s features (such as extension methods) and if you are planning on mixing or dealing with mixed code unfortunately you'll end up dumbing down your c# to accomplish this.

    I don't know about making the case that both languages generate the same cli code but at anyrate its probably a small difference.
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,133
    Not sure...at one point the loop performance of JS wasn't optimal, but they fixed it, although that was before 3.1.

    --Eric
     
  15. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
    It also depends on your programming history.
    I used to code in C/C++, so C# came naturally, and I found Javascript too vague and permisive...
     
  16. Codster

    Codster

    Joined:
    Apr 27, 2010
    Posts:
    440
    Okay I ran my tests and js came out on top (but only just). But I also wondered about boo. And since boo is a ton faster (for me) to write simple scripts I thought I would give it a shot. Anyway from right to left; JS, C#, boo

    P.s. What kind of freak rigs are you guys running? This is a brand new computer with a 3.4ghz i7 processor and I'm getting around 3 seconds... hmm something is going on lol
     

    Attached Files:

  17. Gigiwoo

    Gigiwoo

    Joined:
    Mar 16, 2011
    Posts:
    2,916
    For what it's worth, the only limitation I have found so far in Javascript is the lack of delegates and the poorly implemented function() references. Although I have searched and searched, I have not been able to find any code (that actually works!) that implements a delegate-like behavior for Javascript. ... Other than that, Javascript is working well for me.

    Gigiwoo.
     
  18. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    820
    You'll find more Unity-specific tutorials for UnityScript .The problem with UnityScript (JavaScript) though is that this particular implementation does not exist outside of the world of Unity - where as C# does. You'll find significantly better C# documentation and general programming information out there on the interwebs.

    Once you understand the basic syntax (which honestly, I don't see how C# is any more difficult - it's slightly more verbose in some situations, but that actually makes reading it easier) all the Unity specific information is the exact same function calls anyway, so I really don't see any long term advantage to UnityScript over C# - once you understand the very basics you have a huge amount of information available for one language (C#) and virtually none for another language (UnityScript).

    Really though, it depends on what you want to do. If you want to learn to be a game programmer, I really suggest C# for many reasons. If you want to just develop your own game ideas as quickly as possible in Unity, then I honestly think it's a wash - but since the tutorials are in UnityScript it may be worth going that route if you never plan to really get to the "long term."

    The actual Unity documentation is available in all 3 programming languages now, it's only the older tutorials that are UnityScript specific still.

    Personally, I wouldn't trade a few Unity tutorials for the wealth of information available in C# at MSDN, or the incredibly helpful community over at StackOverflow and other general programming sites.
     
  19. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    18,970
    If you can code in js the leap to c# is nothing measurable, you don't really gain anything from using c# in unity if you are already an accomplished programmer. For example I started with basic, did some COBOL, modula-2, pascal, then did some C++, played with more variants of basic and asm, then some more weird languages like AS2/AS3, etc then went back to C++ / Objective C, and now I'm enjoying unity js.

    I like js with pragma strict on, so I don't slow it down or forget to type variables. Personal preference...
     
  20. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,133
    In Unity, C# is actually annoyingly verbose compared to JS in a number of situations. One thing that affects most scripts is that JS scripts are automatically a class. It's often nice to just write

    Code (csharp):
    1. function Start () {
    2.     // whatever
    3. }
    and have it be a complete functioning script, without a bunch of fluff. Also you can rename scripts without having to worry about the script name matching the class name. It's a small thing but it happens enough to be noticeable.

    JS automatically uses temporary variables for things like

    Code (csharp):
    1. transform.position.x += 1;
    so you don't have to write

    Code (csharp):
    1. var temp = transform.position;
    2. temp.x += 1;
    3. transform.position = temp;
    I use coroutines quite a bit, so it helps that I can just write

    Code (csharp):
    1. yield DoSomething();
    2. yield;
    instead of

    Code (csharp):
    1. yield return StartCoroutine(DoSomething());
    2. yield return null;
    Then there's things like

    Code (csharp):
    1. var go = new GameObject("X", BoxCollider, MeshFilter, MeshRenderer);
    which in C# has to be

    Code (csharp):
    1. var go = new GameObject("X", typeof(BoxCollider), typeof(MeshFilter), typeof(MeshRenderer));
    It seems like there's always some kind of extra "gotcha" like that in C# that doesn't really contribute to readability, but makes it harder to write. I've written thousands of lines of C# code, so I'm quite familiar with it, but in Unity I really prefer JS over C#; it's just easier for me to sit down and write code that works and is readable.

    There are, naturally, things that annoy me about JS too (like not having certain features, so I'm forced to write thousands of lines of C# code...), but overall I prefer it. I generally use C# for "library" code and JS for "game" code. Of course which you like better is a matter of opinion, but there really is a reason for JS to exist in Unity, and it's more than a matter of C# being "slightly more verbose in some situations". If you're going to be really serious with Unity, though, I'd recommend knowing both, unless JS eventually gets those missing features.

    --Eric
     
  21. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    820
    Eric5h5 is one of the handful of people on these forums I would never attempt to argue with; he is flat out a more knowledgeable programmer than I will be for years.

    I really have no doubt that, once you understand both languages well, there is a benefit to using UnityScript in some (many) situations. The main thing I wanted to try and get across (and likely failed) was that C# forces you to learn quite a bit that you can get by without learning in UnityScript. Once you know and understand the advantages to type safety, for example, you'll certainly leverage them in UnityScript as well.

    My problem with UnityScript is how frequently we see new programmers on these forums posting blocks of UnityScript that are difficult for them to follow (and difficult for them to edit) because they are not explicit in their typing, so there's a lot of guessing/checking exactly what data types they are working with. When you see

    Code (csharp):
    1.  
    2. Ray whatever;
    3.  
    You know to just go look at the documentation for "Ray" if you have any questions. Obviously type safe UnityScript could solve the same problem:

    Code (csharp):
    1.  
    2. var whatever : Ray;
    3.  
    But the problem is most new programmers don't seem to be working type safe, so they just end up with

    Code (csharp):
    1.  
    2. var whatever;
    3.  
    For small scripts it's no big deal, but when they start trying to work with large scripts with a lot of moving parts, it seems that a huge portion of their confusion stems from the lack of #pragma strict;

    Another thing that seems to really confuse people is that JavaScript is an inherently a procedural/functional programming language - where as UnityScript is inherently object/component oriented. Class scope is incredibly important to Unity - and it's just another one of those things that UnityScript doesn't explicitly force you to understand to get started.

    UnityScript is very powerful and convenient - but calling it "JavaScript" just seems to confuse a ton of people who are expecting JavaScript but really get C# with different syntax.

    Heck, I think a huge portion of this could be solved by adding #pragma strict,downcast,implicit; to the default generated JavaScript files. People could still remove the pragma if they wanted to, but it would at least force them to acknowledge the purpose it serves.

    But again, it's not that I feel UnityScript is inferior to C# - just that it seems to take new programmers who aren't seasoned enough to understand the benefits of type safety, or concepts like class scope, and makes understanding those things more difficult - or forming bad programming habits easier.
     
  22. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,133
    Heck no. ;) I've spent a lot more time being a graphic designer than being a programmer, although I've reversed that to some extent since discovering Unity several years ago. I just have opinions about stuff.

    One thing with JS and #pragma strict is that if you do iOS or Android programming, you're forced to use it, and since iOS/Android seems to be fairly popular, at least in those cases dynamic typing issues can't confuse things. So I think that helps. (And yeah, it really shouldn't be called Javascript. You could make a case for it with Unity 1, sort of, but not now.)

    --Eric
     
  23. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    461
    Wow - you spent more time as a graphic designer than being a programmer??!!! You just blew my mind.
     
  24. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,133
    9 years, yeah. I can never quite believe people who claim "I'm an artist, I can't do programming."

    --Eric
     
  25. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    184
    Personally, it's all about execution speed to me. Whichever consistently runs faster in the most common scenarios is the one I use. The extra features of C# are just bonuses to me (delegates are awesome!). Of course, I have to take the basic programming tenets into consideration as well, but most high level programming languages follow those anyway so it's not much of a consideration. As long as there's a workaround that doesn't cost an inordinate amount of CPU cycles, then I'm fine with it. Having to type an extra 2-3 lines of code every now and then isn't a consideration to hardcore programmers either; and while I'm nowhere close to being hardcore, I do share that specific outlook. In the end, it's all about implementation of logic and logic problems are fun -- I mean binary and propositional logic, not problems with human thinking like the lack of common sense. XD
     
  26. Gigiwoo

    Gigiwoo

    Joined:
    Mar 16, 2011
    Posts:
    2,916
    Fantastic post Eric!!! I learned a lot reading that.

    So... do you have a recommended solution for the delegate Javascript problem (ie, delegates don't work)? I tried using the function() stuff, but with function(void):void, I couldn't never quite get it to all work out correctly for an OnGUI delegate replacement.

    Gigiwoo
     
  27. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    184
    You should never use OnGUI anyway if you're concerned about your frame rate. XD
     
  28. Gigiwoo

    Gigiwoo

    Joined:
    Mar 16, 2011
    Posts:
    2,916
    Heya,

    I have to disagree. I've been developing software for almost 20 years. C, Basic, C++, Pascal, Java, Javascript, PhP, whatever. Been working in games for almost 8 of those years, so almost exclusively C++ for a long time now. Published chapters in programming books including GPG 6 7 and GEG 2. So, I'm gonna put myself in the 'hardcore' camp.

    Here's why I disagree:

    I enjoying writing code because I enjoy the process of building things. Code, like language, allows me to piece together constructs to build something that user's like. I build things that didn't exist. People use them. I get to watch them smile. I made a difference. That's cool. It's flow while I'm doing it and the results are meaningful accomplishments (see Martin Seligman's Flourish).

    I do not enjoy C++ (despite 10+ years of it) because Every time I turn around, the language forces me to think about minor, obscure nuances. Nuance of pointers, nuance of types, nuance of declaration. Headers, cpps, includes, class definitions. At every turn, it makes me think about the Language, and not what I'm trying to Build. So, yes, 2-3 lines sounds trivial, but it's not. Given that your brain can only reasonably hold 4-7 things at a time, I think the registers in my brain are the ultimate limited resource. I don't want even one of them used to deal with the programming language itself. I want ALL of my big-brain thinking about what I'm building for the user.

    But, other than that, C# and C++ are fine. ;)

    Gigiwoo.
     
  29. Gigiwoo

    Gigiwoo

    Joined:
    Mar 16, 2011
    Posts:
    2,916
    Exactly!

    So, how do others set up a 'delegate-like' system in unityscript? I've done all the searches, I've read dozens of threads on this site and unify. There are always implementation problems in the examples people post of methods like, 'OnGui', which has no params and no return type.

    Gigiwoo.
     
  30. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    184
    I put up with the horrors of Assembly Language (and that was back during the early millenium) so I guess I'm just weird. XD
     
  31. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,133
    I have to say I don't really use delegates enough in game code that I've actually tried to use them in JS.

    --Eric
     
  32. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,832
    I used to respect you.

    Now I'm just plain envious.

    F@#