函数故意有高CPU负载?
我正在制作一个控制其他进程的程序(就停止和启动状态而言)。 标准之一是计算机上的负载低于一定值。
所以我需要一个函数或小程序,它会导致负载非常高以用于测试目的。你能想到什么吗?
谢谢。
I'm making a program which controls other processes (as far as stopped and started states).
One of the criteria is that the load on the computer is under a certain value.
So i need a function or small program which will cause the load to be very high for testing purposes. Can you think of anything?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能想到这个:
I can think of this one :
如果您想在 CPU 上实际生成峰值负载,您通常需要一个适度大小(以便工作集完全适合缓存)的可简单并行任务,有人已手动优化该任务以在处理器上使用向量单元。常见的选择包括 FFT、矩阵乘法和数学向量的基本运算。
这些几乎总是比更多的数论任务(如素性测试)产生更高的功率和计算负载,因为它们本质上是无分支的(除了循环),并且非常均匀,因此它们可以设计为使用完整的计算带宽基本上一直都是一台机器。
用于生成真正最大负载的确切函数随处理器微架构的具体细节变化很大(不同的机器具有不同的负载/存储带宽,与乘法和加法功能单元的数量和宽度成比例) ,但是数值软件库(和信号处理库)是很棒的开始。查看是否有任何针对您的平台进行了手动调整的可用。
If you want to actually generate peak load on a CPU, you typically want a modest-size (so that the working set fits entirely in cache) trivially parallelizable task, that someone has hand-optimized to use the vector unit on a processor. Common choices are things like FFTs, matrix multiplications, and basic operations on mathematical vectors.
These almost always generate much higher power and compute load than do more number-theoretic tasks like primality testing because they are essentially branch-free (other than loops), and are extremely homogeneous, so they can be designed to use the full compute bandwidth of a machine essentially all the time.
The exact function that should be used to generate a true maximum load varies quite a bit with the exact details of the processor micro-architecture (different machines have different load/store bandwidth in proportion to the number and width of multiply and add functional units), but numerical software libraries (and signal processing libraries) are great things to start with. See if any that have been hand tuned for your platform are available.
如果您需要控制 CPU 突发时间,您可以使用类似 Sieve of Eratosthenes(找到素数直到一定数量的算法)并为短突发提供一个小整数(10000),为长突发提供一个大整数(100000000)。
如果您将 I/O 考虑到负载,则可以将筛子中的每个测试写入一个文件。
If you need to control how long will the CPU burst be, you can use something like the Sieve of Eratosthenes (algorithm to find primes until a certain number) and supply a smallish integer (10000) for short bursts, and a big integer (100000000) for long bursts.
If you will take I/O into account for the load, you can write to a file per each test in the sieve.