How to occupy 80% CPU consistently?

后端 未结 3 507
鱼传尺愫
鱼传尺愫 2021-01-04 01:16

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 tri

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 02:12

    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)
    

提交回复
热议问题