Click here to Skip to main content
15,886,003 members
Please Sign up or sign in to vote.
1.58/5 (5 votes)
See more:
So here Im again I´ve solved the last question my self and it executes nisely.
But (there's always a but) when I run the program it increases PC USE like 20% more, and that isn´t good because my tiny laptop cant support 20% more only for a moving "+"

So here´s the loop:

C#
int main()
{
    Board table(3,10); //Initial position of the "+" - Y3, X10
    while(1)
    {
         showGame(table); //Clears the screen and prints map again with changes
         if(GetAsyncKeyState(VK_UP))
         {
             table.moveUp(); //Instructions to move the "+" up
             Sleep(2);
         }
         if(GetAsyncKeyState(VK_DOWN)) 
         {
             table.moveDown();
             Sleep(2);
         }
         if(GetAsyncKeyState(VK_RIGHT))
         {
             table.moveRight();
             Sleep(2);
         }
         if(GetAsyncKeyState(VK_LEFT))
         { 
             table.moveLeft();
             Sleep(2);
         }
         if(GetAsyncKeyState(VK_ESCAPE))
         {
             break;
         }
    }
    return 0;


By putting the Sleep(2) instruction I noticed it decreases the speed on wich the "+" moves (its simple its a + that moves in a 5 x 20 array surroanded by # walls).

The question : How do I decrease the PC use?
Posted
Comments
Kenneth Haugland 15-Aug-12 15:20pm    
Threading and prioroty would be my take...
[no name] 15-Aug-12 15:26pm    
I think that "infinte loop" and "effective" are mutually exclusive terms. Get rid of all the sleeps, you only need 1.
Sergey Alexandrovich Kryukov 15-Aug-12 15:27pm    
Yes, infinite. Meaning, it works for two hours one time, but usually a couple of minutes... :-)
--SA

Indeed, if you use proper Sleep method, you will yield the CPU to other threads. You did not show what is your Sleep, exactly. I hope, this is a right one:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx[^].

Indeed, if a thread calls this function, it is switched off by the OS and put in a special wait state; and it will not be scheduled back to execution effectively wasting zero CPU time until it is waken up; and the wake-up mechanism does not spend any CPU time as it is embedded in the thread scheduler, and a hardware interrupt is involved, in case of timing. The thread can be waken up by different causes. First of all, it will be waken up by the expiration of the sleep time, but be aware that this time can be a little longer then specified, this method is not designed for perfect accuracy, and the OS is not a real-time one. Other methods allows other threads to wake up this thread; such as setting a event object if your waiting thread is waiting at one or more of them.

Now, I don't think it makes any sense in your particular case, if I understand the purpose of your code, of course. It looks like you are trying to develop some kind of UI based on events. Such mechanism do not really require wait. A regular Windows UI already wastes zero CPU time if a user don't activate it or don't press keyboard keys or operate mouse over the activated application window (this includes console window). This is done through GetMessage/DispatchMessage cycle. When a message queue is exhausted, all previously sent messages are processes and the user does not send input, the application wastes zero CPU time (if there are no other thread which may or may not use it despite of the user inactivity).

This works for console applications as well; for example, you can simply call std::cin.get(); and the application will waste zero CPU time until the input is sent. [EDIT] (Here, "cin" means "console input stream".) [END EDIT]

[EDIT]

For example:
C++
char c, str[256];
cout << "Enter the name of an existing text file: ";
cin.get (str,256);
cout << "Enter some character: ";
c = cin.get();

—SA
 
Share this answer
 
v3
Comments
Member 8437747 15-Aug-12 15:56pm    
Could you give me an example using cin.get(). This project is a little excersise program, cause Im new to C++. And what is the best method to use sleep() in my code.
Sergey Alexandrovich Kryukov 15-Aug-12 16:15pm    
Sure, please see the updated answer, after [EDIT].

By the way, I answered your question in full. Please consider accepting it formally (green button) -- thanks.
--SA
pasztorpisti 15-Aug-12 15:58pm    
5ed, Polling VS event based solution: here an event based with a message queue is much better.
Sergey Alexandrovich Kryukov 15-Aug-12 16:17pm    
That's right. Thank you.
--SA
Member 8437747 15-Aug-12 16:40pm    
thanks for the help and your effort just one little question, since I havent get to the input stream in the c++ books I´ve read could you tell whats the str for? and supposing I get right I need to make a couple of if's, could I used this mechanism with the up, down, right, left arrows?
Ill accept your answer friend
Here's a thought... turn your if statements into a proper if, statement and only redraw when there's changes. I bet that will probably eliminate the CPU pegging occurring.
//in pseudo code
main(){
draw();
while(1){
 if(checkkey()){change=true;}
 else if(checkkey()){change=true;}
 else if()...
 
 if(change){ 
   draw();
   change = false;
  }
 }
}
 
Share this answer
 
Another way to minimize cpu use porcentage is showing the map once the key is pressed, that way it goes from 75% down to - 05% in normal use and getting on 35% keeping pressed one key all the time (wich could be just as demanding the past infinite loop, but less than it by a 15% or more)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900