newby performance question... which language is best

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

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.

The performance is the same. They all compile to the same CIL code.

Edit: feh, if only I’d been a few seconds faster. :wink:

–Eric

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.

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.

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.

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)

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.

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.

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.

Those aren’t the results I get at all (click for larger version):

615565--21901--$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

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?

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.

Not sure…at one point the loop performance of JS wasn’t optimal, but they fixed it, although that was before 3.1.

–Eric

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…

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

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.

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.

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…

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

function Start () {
    // whatever
}

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

transform.position.x += 1;

so you don’t have to write

var temp = transform.position;
temp.x += 1;
transform.position = temp;

I use coroutines quite a bit, so it helps that I can just write

yield DoSomething();
yield;

instead of

yield return StartCoroutine(DoSomething());
yield return null;

Then there’s things like

var go = new GameObject("X", BoxCollider, MeshFilter, MeshRenderer);

which in C# has to be

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