加入比较短时间的Sleep()一般对时效性影响不大,但能极大地降低CPU占用。
- 比如,这里我们用coolfun::UI_suspend()等待按键函数的源码来举例:
#include<conio.h>
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
cout << "<< 按任意键继续... ";
for (int i = 0; _kbhit() == 0;) //等待按键
{
switch (i++) //选择打印最后的一个字符,使之有动画效果
{
case 0:cout << "\b/"; break;
case 1:cout << "\b-"; break;
case 2:cout << "\b\\"; break;
case 3:
{
cout << "\b|";
i = 0;
}break;
}
//Sleep(100); //暂停100毫秒(1秒 = 1000毫秒)
}
}
- 效果:
![](https://blog.coolight.cool/wp-content/uploads/2022/04/suspend.gif)
- 但是它的CPU占用相当的高,一直了 8% 左右,而这还仅仅是一个简单的动画功能。
![](https://blog.coolight.cool/wp-content/uploads/2022/04/image-33-1024x215.png)
- 那接下来我们在循环内加一条Sleep():
![](https://blog.coolight.cool/wp-content/uploads/2022/04/image-36.png)
- 然后再来看看它的CPU占用:
![](https://blog.coolight.cool/wp-content/uploads/2022/04/image-37.png)
- 可以看到,显示的是CPU占用 0%,显然是因为占用太低了,所以才显示了 0%。
- 这句Sleep(100),暂停的100毫秒对用户来说基本没感觉,但却大幅度降低了CPU占用,而且动画效果甚至更舒服了:
![](https://blog.coolight.cool/wp-content/uploads/2022/04/suspend-2.gif)
amoxicillin 500mg price: order amoxicillin online no prescription – cost of amoxicillin
Greetings! Very useful advice within this post! It is the little changes that make the greatest changes. Many thanks for sharing!
Your style is very unique in comparison to other folks I have read stuff from. Many thanks for posting when you have the opportunity, Guess I’ll just bookmark this web site.
Pretty! This was a really wonderful post. Thank you for providing this info.
There is certainly a great deal to find out about this topic. I really like all the points you’ve made.
amoxicillin no prescription: buy amoxicillin canada – amoxicillin 500 mg capsule
doxycycline 300 mg price: doxycycline tablets buy online – cheap doxycycline 100mg capsule
buy cipro no rx: buy cipro online canada – cipro