如何持续占用80%的CPU? [英] How to occupy 80% CPU consistently?
问题描述
我正在寻找一种以一致的方式完全占据单个CPU的80%(或任何其他数量)的方法。
对于某些测试触发组件的单元测试,我需要这样做在特定的CPU利用率条件下
为此,我可以假设计算机处于空闲状态。
I'm looking for a way to occupy exactly 80% (or any other number) of a single CPU in a consistent manner.
I need this for some unit test that tests a component that triggers under specific CPU utilization conditions
For this purpose I can assume that the machine is otherwise idle.
什么是健壮且可能独立于操作系统的方法?
What's a robust and possibly OS independent way to do this?
推荐答案
80%的时间都不会占用CPU。 CPU始终处于使用状态或空闲状态。在一段时间内,您可以使平均使用率达到80%。您是否需要一个特定的时间段进行平均?此伪代码应可在各种平台上工作,并且超过1秒的CPU使用率为80%:
There is no such thing as occupying the CPU 80% of the time. The CPU is always either being used, or idle. Over some period of time, you can get average usage to be 80%. Is there a specific time period you want it to be averaged over? This pseudo-code should work across platforms and over 1 second have a CPU usage of 80%:
while True:
startTime = time.now()
while date.now() - startTime < 0.8:
Math.factorial(100) // Or any other computation here
time.sleep(0.2)
这篇关于如何持续占用80%的CPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!