编程之美-1.1 CPU 曲线
解法二:
import time
def cpu_curve():
busyTime = 50 # 50 ms的效果比10ms的效果要好
idleTime = busyTime
startTime = 0
i = 0
while True:
startTime = time.time()*1000 # 得到毫秒级的时间戳
while time.time()*1000 - startTime <= busyTime:
i += 1
time.sleep(busyTime/1000)
cpu_curve()
解法四:
import time
from math import sin
PI = 3.14159265
COUNT = 200 # 用200个点拟合一个周期内的曲线
SPLIT = 2*PI/COUNT # 每个点相隔距离
SIZE = 0.5
OFFSET = 0.5 # SIZE 和 OFFSET将sin(x)的值域转化为[0, 1](可以表示cpu工作时间和休息时间的时间比)
INTERVAL = 100 # 一个点占用的时间为100ms, 100ms比50ms的曲线更加平滑
def SinCurve():
busySpan = [0 for i in range(COUNT)]
idleSpan = [0 for i in range(COUNT)]
radian = 0.0
for i in range(COUNT):
proportion = sin(radian)*SIZE + OFFSET
busySpan[i] = INTERVAL * proportion
idleSpan[i] = INTERVAL - busySpan[i]
radian += SPLIT
startTime = 0
j = 0
i = 0
while True:
j = j % COUNT
startTime = time.time() * 1000
while time.time()*1000 - startTime <= busySpan[j]:
i += 1
time.sleep(idleSpan[j]/1000);
j+=1
SinCurve()
posted on 2019-05-10 13:24 Frank_Allen 阅读(184) 评论(0) 编辑 收藏 举报