Give me an idea about code optimisation. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Give me an idea about code optimisation.

8th Dec 2017, 8:22 PM
subhashini raman
subhashini raman - avatar
7 Answers
+ 4
Code optimisation is the method in which code will be optimize and it will make code easy to understand..
8th Dec 2017, 8:36 PM
Devanshi Patel
Devanshi Patel - avatar
+ 4
Lets look at the following code: x = 5 + 13 * 3; y = 7 + 13 * 3; Optimized code would see the '13 * 3' and only calculate it once using the result twice. Non-optimized code calculates it twice.
8th Dec 2017, 8:51 PM
John Wells
John Wells - avatar
+ 4
Not optimized (inline, repeated checking): ------------------------- code code if(navigator.vibrate) { navigator.vibrate(500); } // verbose, repeat many times, check each time More optimized (less sugar/clutter, same checking) --------------------------- function vib(howlong){ if(navigator.vibrate) navigator.vibrate(howlong); } code code vib(500) // existence still tested each call Even more optimized (1 check, never again -- this, or a lambda) ------------------------------ var vib = function(){} // default null function if(navigator.vibrate) vib = function(howlong){navigator.vibrate(howlong)} code code vib(500); // No more tests; it just does the right thing
8th Dec 2017, 10:09 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
And, as @John mentions, calculation duplication. In the 3D->2D projection formulas at Wikipedia, you can collapse ~50+% of the formulas into reused calculations: https://en.wikipedia.org/wiki/3d_projection#Perspective_projection Starting at "Alternatively, without using matrices..." those formulas have several duplicated items, e.g. (SzY + CzX) ... but as you pull back, there are more. (Yes, this contains trigonometry, but reduction is no worse than algebra, which you do in code already: int a = x+y;).
8th Dec 2017, 10:21 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
thank u...
8th Dec 2017, 8:53 PM
subhashini raman
subhashini raman - avatar
+ 1
yeah...ok..can u explain how to minimize the code to simpler...
8th Dec 2017, 8:40 PM
subhashini raman
subhashini raman - avatar