How To write code in an optimsed way with minimum amount of time taken for compiling and without exceeding the time limit? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How To write code in an optimsed way with minimum amount of time taken for compiling and without exceeding the time limit?

8th Jul 2017, 6:51 PM
Hema Latha
Hema Latha - avatar
4 Answers
+ 4
Thank you#Jamie#Akshay Anurag
10th Jul 2017, 2:42 AM
Hema Latha
Hema Latha - avatar
+ 2
It seems like you are referring to coding competitions. From my experience, I would say: 1. Choose a programming language wisely. Use a compiled language rather than an interpreted one. 2. Don't call functions unnecessarily. As an example, the code: int len = strlen(someString); for(i = 0; i < len; i++) is faster than for(i = 0; i < strlen(someString); i++) as the latter calls a function for each iteration, which is expensive. 3. Use algorithms known to be faster and/or make sure your algorithms are efficient. There are other ways as well and you will only learn them by solving questions and analyzing others' code and spotting the difference.
8th Jul 2017, 8:34 PM
Akshay Anurag
Akshay Anurag - avatar
+ 1
Compile time is dependent on many factors, code being the least. If you mean plain optimization, the compiler does its own. IF you want to write optimised code... *. Reuse variables within the same scope. *. Prefer unrolling for-loops, eg: for (int i = 0; i < 3; i++) do_that(); rather just do: do_that(); do_that(); do_that(); *. Use "clever" structuring. Sadly you need to be able to think out the box and have a grasp of programming behind programming for this. Bottom line, experience is all that'll teach you more than some memorised tricks. Take home: Optimizing compiled code is pretty pointless these days. A modern compiler would have done almost everything in my examples.
9th Jul 2017, 12:05 AM
Jamie
Jamie - avatar
+ 1
You're welcome, @Hema Latha
10th Jul 2017, 2:16 PM
Akshay Anurag
Akshay Anurag - avatar