+ 2
How To write code in an optimsed way with minimum amount of time taken for compiling and without exceeding the time limit?
4 Antworten
+ 4
Thank you#Jamie#Akshay Anurag
+ 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.
+ 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.
+ 1
You're welcome, @Hema Latha