r/C_Programming • u/Samuel_Bouchard • Jun 11 '26
Help me I'm noob
Hello, I'm noob (sorry bad english).
I come from javascript.
I need help.
Here is my code:
#include <stdio.h>
int debug_log(int n){
printf("%d\\n", n);
}
int main(){
int i = 0;
while (i < 1000000){
debug_log(i);
i = i + 2;
}
}I started by adding 1 to i every loop time, but it was slow. It was run for 119 seconds.
Then I decided to add 2 for more performance (optimization trick I learned) and it was run for 62 seconds.
The problem: it was faster (that is what I wanted) but now the log don't work well anymore. Now it shows more numbers. Is it because my computer's ram is stuck on first number or do I need to clear the cache before adding 2? I found no stack overflow subject on it. Do you also had this problem before? I'm on Windows 11. I'm scared I will break the compiler forever if I keep doing it.
Please help.
Update: I learned that maybe it's nvidia bugged or C performance issue. Also why downvote I'm just noob?
Update2: I don't think it from GPU anymore but I still don't understand. Why is log slow?
6
u/HashDefTrueFalse Jun 11 '26 edited Jun 11 '26
I don't understand what your actual question is. Perhaps you could add example before and after output to illustrate your problem?
You're going twice as fast because you're doing half the work (you skip printing every other number). There's no optimisation trick here.
You won't. That's not how compilation works. Compilation inputs don't permanently alter the compiler and compilers can deal with malformed programs just fine by and large.
Also your code has several issues (missing returns, incorrect main signature...). I'd list them but it's late.
Edit:
Oh. I think you must be trolling because this is definitely not what you were told here.
4
u/dychmygol Jun 11 '26
One sure way to slow a program down is to print to console in a loop.
Caching has nothing to do with it.
You can't break your compiler by compiling.
Incrementing by two instead of incrementing by one is not an "optimization trick."
There's no clear indication of what it is you want to do.
3
u/MyTinyHappyPlace Jun 11 '26
I don't understand your problem. Are numbers missing in the output?
How is adding 2 an optimization trick if you want to print all numbers?
Not sure how you are coding, but please activate all warnings and read them carefully. debug_log() is supposed to return an int in your code.
You are not going to break your compiler with some loops. You don't have to clear any cache.
0
u/[deleted] Jun 11 '26
[deleted] — view deleted comment
0
u/[deleted] Jun 11 '26
[deleted] — view deleted comment
2
u/[deleted] Jun 11 '26
[removed] — view removed comment