我想用c++写一个程序,它能够支持windows7系统。在我的意图中,我希望这个程序带来的cpu使用率为100%,使用所有安装的内存。
我已经尝试了一个大的FOR循环,它在每一步都运行一个简单的乘法: cpu使用率增加,但ram使用率仍然很低。
达到目标的最佳方法是什么?!
发布于 2014-11-06 00:53:20
以一种与操作系统无关的方式,您可以分配和填充通过malloc(3)获得的堆内存(因此使用该区域执行一些计算),或者在C++中使用operator new
进行一些计算。请确保针对malloc
故障进行测试。并逐步增加区域的大小。
发布于 2014-11-23 22:58:58
如果您目标是能够提高CPU和内存的利用率(而不一定是编写程序),那么可以尝试使用HeavyLoad,它是免费的,只执行http://www.jam-software.com/heavyload/
发布于 2019-05-01 22:48:16
我写了一个测试程序,使用多线程和梅森素数作为压力测试的算法
代码首先确定机器上的核心数量(仅限WINDOWS的家伙,对不起!)
NtQuerySystemInformation(SystemBasicInformation, &BasicInformation, sizeof(SYSTEM_BASIC_INFORMATION), NULL);
然后,它运行一个测试方法,为每个内核派生一个线程
// make sure we've got at least as many threads as cores
for (int i = 0; i < this->BasicInformation.NumberOfProcessors; i++)
{
threads.push_back(thread(&CpuBenchmark::MersennePrimes, CpuBenchmark()));
}
运行我们的负载
BOOL CpuBenchmark::IsPrime(ULONG64 n) // function determines if the number n is prime.
{
for (ULONG64 i = 2; i * i < n; i++)
if (n % i == 0)
return false;
return true;
}
ULONG64 CpuBenchmark::Power2(ULONG64 n) //function returns 2 raised to power of n
{
ULONG64 square = 1;
for (ULONG64 i = 0; i < n; i++)
{
square *= 2;
}
return square;
}
VOID CpuBenchmark::MersennePrimes()
{
ULONG64 i;
ULONG64 n;
for (n = 2; n <= 61; n++)
{
if (IsPrime(n) == 1)
{
i = Power2(n) - 1;
if (IsPrime(i) == 1) {
// dont care just want to stress the CPU
}
}
}
}
并等待所有线程完成。
// wait for them all to finish
for (auto& th : threads)
th.join();
我的博客网站上有完整的文章和源代码
http://malcolmswaine.com/c-cpu-stress-test-algorithm-mersenne-primes/
https://stackoverflow.com/questions/26762644
复制相似问题