Somewhere I remember reading in the Unity docs that C# had a (significant?) performance benefit over Javascript on the iPhone. However, I have been unable to find the exact line in the docs to confirm this.
I’ve been programming a long time, and either language is equally accessible to me. My game will involve a large number of calls to short functions in the scripts. So, if there’s an advantage in function calls for one over the other that could be significant.
So, excluding any preferences for syntax, etc, is there a performance difference?
That’s exactly the line I was remembering, and so now I see that its not speed but build size that is affected.
Thanks very much! I knew there was a reason I wanted to consider C#, now I’ll have to figure out if its sufficient to make me choose it over Javascript.
hmm - with unity messaging i assume you mean Gameobject.Sendmessage() ?. Do you have some data/in depth info to share HOW slow this thing really is ? I tend to use this mechanism only when i’m in a lazy mood - so if it’s really that slow i might stop using it completley
If you don’t mind either one, I would go with C#. I have no (responsible measurable) evidence, but think C#, as the native language for Mono, has preferred development time. I think it has the best compiler and does better optimizations. C# semantics should be better recognized by the IL and thus generate better native code. The inherently safer type system of C# should help it make better assumptions.
Since other .Net languages are written in C#, I can’t help to think that any other language is just catching up with C#. In theory there should be no difference. I think there are differences, and C# has the best compiler and generates the best code.
But I could be wrong And in most cases, the performance benefit will not outweigh javascript’s ease of use.
Sorry to correct but there is no difference unless you duck type in JS. iPhone won’t let you do that tho. So that only applies to PC/Mac/Webby. Use whichever one works for your team.
[edit: C# does have some things like dimensional arrays that are kind of a pia in JS. So there are other things to consider but performance really isn’t one of them.]
This is a strong assertion. For the benefit of me and others, can you point me to the facts? I know most people agree. But how can different compilers (developed by different groups with different man power) generate the same optimized code.
We’ve being plagued with different C\C++ compilers generating different code under same environments. An its widely known which compiler is better under/for what environments.
I would love to make some time to check optimized IL code generated by these languages, as I’m always in the lookout to squeeze the last bit of performance.
With all due respect, where can we find this to be verified truth?
Dynamic typing in JS is slower. If you strict type there is no difference. In Unity iPhone you can’t dynamic type because of AOT. So for iPhone it doesn’t apply. As far as we’re concerned here, there’s only one compiler - Unity’s. It says no diff unless you duck type in JS.
Search the forums too. It’s been discussed a lot but if you need “horse’s mouth” proof, there’s a Unite video about optimization. IIRC about 30-ish minutes in there’s a discussion of C# vs JS where it’s stated there’s no difference unless you duck type.
there are two compilers actually for iphone: Unitys (for Unity Script) and the Mono 1.2.5 C# compiler.
But perhaps you talk about the AOT endcompilation but that only translates cil to iphone ASM, nothing else. if the CIL code is slower, then its just slower and thats bound to the first stage compiler.
the primary benefit of C# over JS is that you don’t need the boo and js core assemblies, so you have a smaller and and thus less initial loading time.
But other than that, I’m not aware of any benchmark that gives clear numbers over several usage scenarios on which side is faster on the iphone actually
[edit: And yeah I meant end compilation. From what I understand there’s no difference in code execution on the device as long as you strict type in JS.]
My concern is the first stage from language to IL, where the language differences would show up. And yes, assuming strict javascript.
Granted this might not be of importance for neo and probably less than 1% of other unity users. But me and maybe that 1% of Unity users want their game to run at 60 fps and will take no less. So every bit of info is of importance. [Just in case neo feels his thread hijacked, sorry]
Close to release (and if at all possible) I might do some last IL optimizations and will report any findings.
Yes - sorry for the derail neo! Hope you find it useful anyway.
@rozgo - I believe strict typing gives you the same out of the first stage but I don’t think anyone but UT can give you an emphatic answer. As dreamora said, I don’t think there are any solid benchmarks available either. 60fps… rock on!
This is an interesting discussion and like dreamora said there’s the difference in size with not requiring the JS assemblies
IMHO, it’s actually fairly difficult to do a real test. For example, when looking at a C/C++ compiler compiling for a familiar assembly language (ARM, MIPS, x86 etc) there are certain obvious things to look for. How does the compiler make use of the registers, how does it treat arithmetic and so on. But with the CIL unless you either know it or can see what’s being produced, it’s almost impossible to say. You’re then comparing how good the virtual machine is, if there’s a JIT compiler and how well that is mapped to the processor architecture. Not to mention how good the AOT is at generating ARM code. Historically, C/C++ compiler writers and processor designers have pretty much worked together to get the best performance, e.g. using the many registers in a RISC chip to pass function arguments so as to avoid hitting much slower memory or having massive L1 caches in the case of the x86 CISC design. Not to even begin to get into instruction pipeline optimisations. We’re also assuming the frameworks and Unity side of things are not a bottleneck. And then there’s the other related issues like boxing, strings, foreach and garbage collection.
With a trivial test script running on the Mac (and therefore removing any CIL to ARM compilation) I see a very slight difference with C# coming out on top, but it’s very minor and could possibly just be down to statistical errors.
So in that case you may need a real-life example, a game coded entirely in C# compared to the same thing re-coded in JS.
I believe I may be in the one % where it matters. And I think for my game the call speed is most relevant. EG: Scripts calling other scripts (or “Sending messages” in Objective-C parlance) and unity calling scripts (like OnCollissionEnter).
I will pick a language for a small percentage gain in performance here.
I normally attach objects to each other-- they have a reference to the transform as a local variable and then from there they get the script and then call methods on it.
I’d appreciate some pointers to the most efficient way to do these method calls, though. And am still open to the possibility that C# is faster than javascript. (And believe me, I have an anti-microsoft bias, and see C# in a lesser light because of it, so don’t think I’m a C# fan.)
I don’t know about the iPhone, but I did a little benchmarking using desktop Unity a while ago, and generally they are the same. What differences there are, aren’t worth worrying about–you’d be talking 60.01 fps vs. 60.0 fps, basically.
I did find a couple cases where there was a measurable difference, however 1) these were small artificial benchmarks, not “real” code and 2) in one case C# had the edge, but in the other case, JS did. So I wouldn’t make any assumptions or go by “I think” or “I feel”…if it’s really that important, do actual benchmarks, but I’m pretty sure you won’t find any noticeable difference with production code. At least, I didn’t, but who knows, maybe I missed something. Also, this…JS isn’t stagnating, it seems.
Diggin up the dead threads! This is for rozgo, zibba and well, anyone else who’s interested.
I decided to port our game to C# tonight to give the C# vs JS debate a firsthand look see. I prefer JS but if I can eek a little more speed, C#'s ok then!
Anyway, I can’t say this is empirical. Our game generates random levels and it’s a bit tough to line up gameplay exactly. However it’s always the same number of objects and scripts. The easiest way to compare was a screen grab. Sorry if the image is tough to read or if you’re on a small monitor. Take a look in Preview (or ImageViewer - Windows equiv?) if your so inclined. If you don’t want to bother, I’ll just say the numbers are identical across the board. If nothing else, it’s at least compelling.
yeah I knew you’d like it. didn’t mean to not include you or neo or anyone else on who it was for. i did hope C# would drop me to 16 ms but sigh no such luck