- Home /
Script Performance Questions
Hello there, the Eldars of Unity Community,
After a year of coding, now I have enough experience but if you know
more, you realise you need to know alot more so I have few questions:
I use Javascript and I work on IOS so I need performance, do I have to use C++ in some of the calculations to improve the performance?
I have a lot of coding from previous projects and I want to update them. I know most of the tricks for performance but scripting tricks I am lack of. I have many codes like if(a==b && b==c && c==d && d==e) and it can be changed like if(a==b){if(b==c && c==d && d==e)} so it can be optimised but is this performance optimisation important or is it such a small gain that I do not need to change it?
And it would be easier for me to use if statements alot but if I work on them I can make some shorter with extra effort and smarter coding, I had some ugly coding at past but the performance will change in a way that I would care? Is the if statement very fast?, in this way I mean there are some crazy expensive stuff like FindObject, in compare with those, is if statement expensive in this means or not, like for example is it 10 times cheaper than FindObject or 100 times cheaper? I would calculate my performance needs better if I would have some data, some stuff you cannot learn efficiently from profile, I hope you got what I mean.
Thank you community, sorry for my English and hope for an answer from a person with exact knowledge.
Answer by Jessy · Jun 14, 2011 at 02:10 PM
You can't script in C++.
That's not an optimization at all. && short-circuits, unlike &.
An if statement is a vital part of coding and you're not going to get rid of it. Write as much code as you need to; if statements are going to have to be a large part of that. if statement are not likely to be bottlenecks.
you could script in c++ using plugins (pro only), but that wouldn't be an optimization at all : ]
Sorry, my mistake, I mean to use C++ directly in Unity ins$$anonymous$$d of Javascript. I heard some loops are much more faster but i couldnt find an exact information.
And according to Jessy, if statements are real fast so I should not think to code more in case of if statements, thats good news, thank you.
Answer by hellcats · Jun 14, 2011 at 03:28 PM
Understanding and predicting performance isn't easy in general, and a managed environment like Mono makes it even harder. But IMHO the best thing you can do is improve your knowledge of computer hardware and runtime environments. Learn to read assembly language (at least x86), and then get the Mono source code. Look at the IL output from the compiler, then look at how that is JITed into x86 by the Mono runtime. Learn about calling conventions, stack variables vs. heap variables, return results, etc. Get the intel developers guide http://www.intel.com/products/processor/manuals/ . This may sound like a lot, and it is, but this kind of understanding will serve you throughout your entire career.
Answer by Bravini · Jun 14, 2011 at 10:47 PM
complementing Jessy and helicats answer, there is a lot of optimization that can be done, and much of it needs further studying and technics. A good place to start is http://unity3d.com/support/documentation/Manual/Optimizing%20Graphics%20Performance.html a tip would be to use getcomponents instead of findGameObjects, but you can find lots of good learning material on the forum or answers from more seasoned developers like Jessy, Erich5h5, Duck, DaveA, Statement, skovacs1, etc that discuss more in depth stuff, just browse their answered questions through the profile and you can learn a lot, I do that once in a while ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220612104652im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
if statement help! 2 Answers
C# Creating a List of If Statements 3 Answers
Script block animations and other scripts 2 Answers
Create file in correct directory 3 Answers
Problems with if statements 2 Answers