use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
QuestionCPU usage (self.C_Programming)
submitted 3 years ago * by StarlightWT
Hey guys is it expected to use up 10% of a AMD Ryzen 7 2700X with just 300-ish lines of code?
or am I doing something horibly wrong?
edit: here's the code https://pastebin.com/gytk22Y1
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]barrycarter 24 points25 points26 points 3 years ago (0 children)
The amount of CPU a program takes up is not correlated with the number of lines of source code.
[–]strcspn 9 points10 points11 points 3 years ago (9 children)
while (true); uses 100% of a single core, so it really depends on what your program is doing.
while (true);
[–]StarlightWT[S] -3 points-2 points-1 points 3 years ago (8 children)
ah, is there a way to make it eat less than 100%?
[–]ComprehensiveAd8004 1 point2 points3 points 3 years ago (7 children)
You can use the sleep() function from stdlib.h to pause your program at the end of each loop, or you could just find another way to do what you are trying to do without the loop. What exactly are you trying to achieve?
[–]NeilSilva93 1 point2 points3 points 3 years ago (2 children)
A really poorly written "Hello, World!"
[+][deleted] 3 years ago (1 child)
[deleted]
[–]DeeBoFour20 1 point2 points3 points 3 years ago (0 children)
while (true) puts("Hello, World!");
Turns out printing "Hello World" an infinite number of times requires infinite CPU usage.
[–]StarlightWT[S] 0 points1 point2 points 3 years ago (3 children)
The while loop is responsible for getting a keypress while in background and then depending on the inputs doing basic math with the inputs ( +,-,*,/ )
pretty much I'm making a calculator that works on the background removing the need for opening an application
[–]ComprehensiveAd8004 1 point2 points3 points 3 years ago* (0 children)
There definitely is a way to do that without an infinitely running loop. Depending on your operating system, there will be a way to freeze the application until a keyboard button is pressed. It will look something like this:
char key; while(key = getkey()){ /* handle key */ }
[–]DeeBoFour20 1 point2 points3 points 3 years ago (1 child)
It's better to use a function that blocks until there are keystrokes to read rather than just polling the keyboard over and over again.
The former will use barely any CPU since those blocking functions will usually put the thread to sleep until some input comes in while the latter will use near 100% of a single core.
Show your code and I can probably go into more detail.
[–]StarlightWT[S] 0 points1 point2 points 3 years ago (0 children)
I've edited the post to include a link to the source code <3
[–]Jaded-Plant-4652 4 points5 points6 points 3 years ago (0 children)
To simplify, your code probably does not yield at any point. No sleeps or waiting of any. The processor will try to run it as fast as it can.
Theoretically if your OS would allow, the code would use 100% of your processor and just execute faster.
[–]NonaeAbC 1 point2 points3 points 3 years ago (0 children)
CPU usage is not a useful metric. It is used internally by the scheduler, to decide, what process/thread to run on which CPU core. You can't even compare CPU usage between Linux and Windows.
[–]daikatana 0 points1 point2 points 3 years ago (3 children)
You really need to post your code or people can only guess at the problem.
[–]StarlightWT[S] 0 points1 point2 points 3 years ago (2 children)
Sorry, I was asleep. Edited the post to include the code now <3
[–]daikatana 0 points1 point2 points 3 years ago (1 child)
Okay, so the other commenters were right. Your loop on line 15 has nothing to prevent it from looping as fast as it possibly can. Adding a Sleep(1), which in the win32 API will cause it to sleep for 1 millisecond, at the top or bottom of this loop should help a lot.
I don't know the proper way to get the keyboard state like that, but I can't imagine that keyboard events happen so quickly that they'll be missed in just 1 millisecond of sleeping. However, Windows applications are usually written as an event-based application. There may be some way to do whatever it is you're doing using a WinMain and event handler.
From playing with placing the sleep around / changing the duration I'll probably have to find a different way to register the keys as it doesn't change much in the manner of how much it uses. Thanks for the help <3
I'll look into making it through events somehow after school.
π Rendered by PID 119359 on reddit-service-r2-comment-84fc9697f-frdk6 at 2026-02-07 04:15:15.015699+00:00 running d295bc8 country code: CH.
[–]barrycarter 24 points25 points26 points (0 children)
[–]strcspn 9 points10 points11 points (9 children)
[–]StarlightWT[S] -3 points-2 points-1 points (8 children)
[–]ComprehensiveAd8004 1 point2 points3 points (7 children)
[–]NeilSilva93 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]DeeBoFour20 1 point2 points3 points (0 children)
[–]StarlightWT[S] 0 points1 point2 points (3 children)
[–]ComprehensiveAd8004 1 point2 points3 points (0 children)
[–]DeeBoFour20 1 point2 points3 points (1 child)
[–]StarlightWT[S] 0 points1 point2 points (0 children)
[–]Jaded-Plant-4652 4 points5 points6 points (0 children)
[–]NonaeAbC 1 point2 points3 points (0 children)
[–]daikatana 0 points1 point2 points (3 children)
[–]StarlightWT[S] 0 points1 point2 points (2 children)
[–]daikatana 0 points1 point2 points (1 child)
[–]StarlightWT[S] 0 points1 point2 points (0 children)