有效执行无限循环 [英] Execute a Infinite Loop effectively

查看:89
本文介绍了有效执行无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在这里,我再次解决了我自己的最后一个问题,它很好地执行了.
但是(总是有一个but),当我运行该程序时,它会使PC的使用率增加20%以上,但这并不好,因为我的小型笔记本电脑仅对移动的"+"不能再支持20%.

所以这是循环:

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:

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;



通过放置Sleep(2)指令,我注意到它降低了"+"移动的速度(其简单的是a +在#个壁围成的5 x 20阵列中移动).

问题:如何减少PC使用量?



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?

推荐答案

实际上,如果使用正确的Sleep方法,则会将CPU分配给其他线程.确实,您没有显示什么是Sleep.希望这是对的:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms686298%28v=vs.85%29.aspx [


例如:
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. (Here, "cin" means "console input stream".) [END EDIT]



For example:
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


这里有个想法……将您的if语句转换为适当的if,语句,并且仅在有更改时才重绘.我敢打赌,这可能会消除CPU挂起的情况.
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;
  }
 }
}


使cpu使用率最小化的另一种方法是在按下该键时显示地图,即从正常使用的75%降到-05%并继续35始终保持按下一个键的百分比(这可能与要求过去的无限循环相同,但比过去的无限循环要少15%或更多)
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)


这篇关于有效执行无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆