如何模拟100%CPU使用率? [英] How do I simulate 100% CPU usage?
问题描述
为了模拟100%的CPU使用率,我在代码中放置了一个无限的while循环(while(true){})。
这似乎使CPU使用率飙升至30%(对于没有while循环运行的同一程序,通常为2%)。
为什么不超过30%?这是双核Intel i7处理器。该应用程序是一个简单的控制台应用程序,在.net 4.0上运行c#代码
To simulate 100% CPU usage, I placed an infinite while loop in my code ( while (true) { } ) . This seemed to spike the CPU usage up to 30% (ordinarily it is 2% for the same program that I run without the while loop). Why does it not go above 30%? This is a dual core Intel i7 processor. The app is a simple console app running c# code on .net 4.0
private static void SimulateCPUSpike()
{
while(true) { }
}
推荐答案
CPU使用率是所有个CPU内核的百分比。
如果您的代码仅运行一个线程,则不能占用多个内核。
CPU usage is a percentage of all CPU cores.
If your code is only running a single thread, it cannot occupy more than one core.
您需要为每个核心创建一个单独的线程。 ( Environment.ProcessorCount
)
You need to make a separate thread for each core. (Environment.ProcessorCount
)
这篇关于如何模拟100%CPU使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!