Why sometimes 100 lines of code is better than 2 lines of code?

Table of contents
On what basis do we compare on code is better than the other?
Growth Rate and Time Complexity
Conclusion
Example
1. On what basis do we compare one code is better than the other?
So, when we say that one code is better than the other (for same problem statement). What do we mean by betterment here? No, it’s not about the run-time of code. Run-time depends on external factors like OS, processor, network connection etc.
We simply know one thing, the more number of instructions to be executed means the more time it will take to run. So, for any solution we want our code/program to be fastest i.e., execute less number of instructions in run time.
So, the program which executes less number of codes during it’s execution is considered better (efficient) than other programs. It’s not the number of instructions written (number of lines of code) in the program but the number of instructions that goes under execution that matters.
one should know that it takes only few milli-seconds for a program (with hundreds of insturctions) to get executed. i.e., small input sizes are of no use in comparison. Hence, we talk about effieciency of a program at large input size.
We want our number of instructions to be less, even for large input size. That’s how a program becomes efficient.
2. Time Complexity and Growth Rate
Time-Complexity is the measure of basic operations to be performed during execution and also how it grows with increase in input size. It tells about the efficiency of a program.
Now, we need time complexity just to compare the efiiciency of programs. So, taking some assumptions we give it an absolute value. Then, it will be easy for us to compare between time-complexity of different programs.
Basic Operation: any operation of comparison, arithmetic opration, printing, taking input, declaration, initialization etc. in the program is basic operation and will be counted as single/one operation.
Assumptions:
Each basic operation takes one milli-second for execution.
Each basic operation takes equal time for execution
hence, for n basic operations, time-complexity is n.
for time complexity of format ‘an+b’ , a & b can be ignored. (large input size)
Growth Rate: Increase in the number of instructions of program with increase in input size is termed as growth rate. We want this growth rate to be as less as possible.
3. Conclusion
For an efficient program, we want it’s time-complexity to be lesser than others. Moreover, we want it’s growth rate to be slower.
In this way, we can conclude that 100 lines of code can be better than 2 lines of code sometimes. Because, 100 lines of code does not ensure that all the lines of code (instructions) are actively participating in execution.
While 2 lines of code (say loop) can execute hundreds of instructions.
4. Example
See example: Let’s say we have 10 stones, in which 9 stones have same weight (1 kg) and 1 stone is heavier (10 kg) than others. Find that heavy stone. Assume an array.
Solution: See both the programs, first program which has less lines of code but once loop starts it will execute around 30-35 instructions(basic operations). (considering heavier ball at index 9)
while second program, which has much more lines of code but executes only 10-12 basic operations under same condition.
and this gap between number of instructions getting executed of both programs will increase with increase in number of balls.
hence, number of lines of code in a program does not decide the effieciecny of a program but number of instructions actively participating in execution (number of basic operations being performed) does.
i.e., It’s time-complexity which decides the efficiency of a program not number of lines of code or run-time of program.
#include<iostream>
using namespace std;
int main(){
int heavyIndex;
for(int i=0; i<10; i++){
if(a[i]>1){
heavyIndex=i;
break;
}
}
cout<<heavyIndex;
return 0;
}
#include<iostream>
using namespace std;
int main(){
int heavyIndex;
if(a[0]+a[1]>2){
if(a[0]>a[1]){
heavyIndex=0;
}
else{
heavyIndex=1;
}
}
if(a[2]+a[3]>2){
if(a[2]>a[3]){
heavyIndex=2;
}
else{
heavyIndex=3;
}
}
if(a[4]+a[5]>2){
if(a[4]>a[5]){
heavyIndex=4;
}
else{
heavyIndex=5;
}
}
if(a[6]+a[7]>2){
if(a[6]>a[7]){
heavyIndex=6;
}
else{
heavyIndex=7;
}
}
if(a[8]+a[9]>2){
if(a[8]>a[9]){
heavyIndex=8;
}
else{
heavyIndex=9;
}
}
cout<<heavyIndex;
}
Subscribe to my newsletter
Read articles from Yatharth Baranwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
