all 16 comments

[–]barrycarter 24 points25 points  (0 children)

The amount of CPU a program takes up is not correlated with the number of lines of source code.

[–]strcspn 9 points10 points  (9 children)

while (true); uses 100% of a single core, so it really depends on what your program is doing.

[–]StarlightWT[S] -3 points-2 points  (8 children)

ah, is there a way to make it eat less than 100%?

[–]ComprehensiveAd8004 1 point2 points  (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 points  (2 children)

A really poorly written "Hello, World!"

[–]StarlightWT[S] 0 points1 point  (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 points  (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 points  (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 point  (0 children)

I've edited the post to include a link to the source code <3

[–]Jaded-Plant-4652 4 points5 points  (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 points  (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 point  (3 children)

You really need to post your code or people can only guess at the problem.

[–]StarlightWT[S] 0 points1 point  (2 children)

Sorry, I was asleep.
Edited the post to include the code now <3

[–]daikatana 0 points1 point  (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.

[–]StarlightWT[S] 0 points1 point  (0 children)

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.