r/learnprogramming • u/Scary-Combination-67 • 14d ago
How do you all improve code.
While solving a CodeForce problem, I know this problem is very basic and easy, I was just getting started.
But while solving I thought about this. How could I improve my code and make it more efficient and fast.
If you want to improve a code and make it more efficient and fast what you exactly think to change and why that change will affect the performance.
I know it does depends upon the problem itself(Like that's why there are more version of a program rather than just V1; V2, V3... is improved in terms of performance, etc. so I know it does takes time), but still.. for a small problem like this one. How could you think to improve it?
This problem there are many ways to solve it, look at this
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
switch (n % 2) {
case 0:
printf("YES");
// Instead of using break, I used this return.
// Cause I thought if I use break the program,
// the program will leave this switch statement,
// then go down and check if any statement to execute.
// But, here, It has none so if I just use return 0,
// I could improve it's performace(I don't know it does helps or not)
return 0;
default:
printf("NO");
return 0;
}
return 0;
}
I know the last statement won't get executed but still if in real life if everyone uses this approach (if there is no code below), won't it be a little faster. Like not only use this but these kind of approaches.
Like in python I would do something like this.
print("YES" if int(input()) % 2 == 0 else "NO")
If I used a standard or like normal approach in this way. I think it's slower. Cause it needs to create memory for n and also free it up. But in above it does not need to allocate or free memory for any number(I think), unlike this below one. Like think my program has more code to execute but it does not need the value of n(this input) and in the below it won't free memory of n after the code but the above one it will(it won't even store it)
n = int(input())
if n % 2 == 0:
print("YES")
else:
print("NO")
Like is there still any way to improve it. I just want to solve it, not validate the inputs, etc.
View all comments
7
u/Substitute_Hecate 14d ago
One thing that'll warp your brain a bit is realizing the compiler is smarter than you. It'll optimize away that extra variable in the C version too, so you're effectively debating syntax sugar at that level. The real difference is negligible until you're looping it millions of times.
For Python, your one-liner avoids the name binding but honestly the garbage collector doesn't work in real-time like you're imagining. The memory gets reclaimed almost instantly either way because the reference count drops to zero.
If you really want to shave nanoseconds on this specific problem, you could read the input as bytes and check the last bit of the last character before the newline. But at that point you're writing unreadable code for an 800-rated problem, which is a terrible habit to build early on.
The habit you should build instead is profiling before you optimize. Guessing what's slow is a trap that leads to wasted time and ugly code. Write it clean first, then measure.