编写 CPU 绑定脚本来粗略测量 CPU 性能
我编写了一个脚本并在不同的机器上运行它。脚本如下所示
def f(n):
x = None
while n:
x = simple_math(n)
n -= 1
return x
start = now()
f(BIGNUM)
print now() - start
在脚本末尾,它打印完成需要多长时间。这是否足以比较不同机器的简单 Python 脚本的实际 CPU 速度?
简单地说,我的意思是它不使用多处理模块或任何其他技术来利用多核机器。
这个问题不是关于
- 让 python 程序运行得更快,
- 多处理模块
- GIL、I/O 效率等
- 非 cPython 程序
只是我想确定我理解机器之间 CPU 性能的方法是否相当正确。
I have wrote a script and running it on different machines. Script looks like below
def f(n):
x = None
while n:
x = simple_math(n)
n -= 1
return x
start = now()
f(BIGNUM)
print now() - start
At the end of the script it print how much time does it take to finish. Is this good enough to compare different machine for practical CPU speed for simple Python scripts?
By simple I mean it does not use multiprocessing module or any other technique to take advantage of multi-core machines.
This question is not about
- making python programs run faster
- multiprocessing module
- GIL, I/O efficiency etc.
- non cPython programs
Just that I want make sure if my approach to understand CPU performance among machines is fairly correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无数现有基准有什么问题?更复杂的可能更强大一些。我(请注意,我不是这个主题的专家)可以发现您幼稚方法的主要问题是:
但是,由于您的要求似乎只是“对 CPU 速度的非常粗略的估计”,因此充分意识到这些数字除了将 CPU 速度放入数量级之外不能用于任何其他用途,因此即使在那时也必须持保留态度,并且不要“不要告诉任何有关任何实际应用程序的实际性能的信息”,这可能没问题 - 只是不要认为它接近准确。尽管如此,为什么不使用一个强化的基准测试套件,它已经付出了一些努力来减轻(而不是消除 - 没有人能做到这一点)这些问题呢?
另请注意,
timeit
stdlib 模块比手动使用秒表更容易使用,并且尝试(不是太难,但这是一个开始)通过我提到的方法修复第二点。What's wrong with all of the countless existing benchmarks? The more sophisticated ones are propably a bit more robust. The major problems of your naive approach I - and I'm not an expert on this topic, mind you - can spot are:
But since your requirements seem to be "very rough estimate of CPU speed only, in full awareness that these numbers can't be used for anything except putting CPU speed into orders of magnitude, must be taken with a grain of salt even then and don't tell anything about the actual performance of any real applications", it might be okay - just don't consider it anywhere close to accurate. Still, why not use a hardened benchmark suite that already put some effort into mitigating (not removing - nobody can do that) these problems?
Also note that the
timeit
stdlib module is both easier to use than manually wielding the stopwatch and tries (not too hard, but it's a start) to fix the second point by the method I mentioned.通过使用这些类型的方法,您可以得到一个粗略的想法。但这并不是精确的测量。脚本的执行时间将取决于除CPU速度之外的许多其他因素,例如使用的操作系统和解释器版本、当前系统负载、内存速度等。我的建议是不要依赖于此。
编辑:只是一个注释。当谈到性能时,许多人只考虑 CPU 速度,但实际上系统上的几乎所有内容都会阻碍性能。例如,您有一个高速 CPU,但 RAM 较低(无论是大小还是速度),那么您将无法获得 CPU 的性能提升。
You can get a rough idea by using these type of methods. But that will not be exact measurement. The execution time of the script will depend on many other things other than CPU speed, like OS and interpreter version used, current system load, memory speed etc. etc. My suggestion is not to depend on this.
EDIT: Just a note. When it comes to performance, many people think only about CPU speed, but actually performance can be hampered by almost everything on the system. For example you have a high speed CPU but low RAM (both in size and speed), then you will get no performance boost up for the CPU.
本质上:不。
基准测试是一个非常困难的问题,通常不值得您自己解决。这完全取决于你为什么关心。您的方法肯定会非常粗略地估计系统 A 是否优于系统 B,但实际上只有当结果截然不同时。
您想要做的是确定真实世界应用程序 X 在不同计算机上的执行情况。现实世界的应用程序很少用简单的数学循环来近似。即使是这样(主要是科学计算),您最好还是测量实际程序的时间。
现实世界的应用通常是非线性的,并且难以测量和模拟。这确实是别人解决的问题比你自己解决的要好得多的问题之一。
如果您想要对性能进行非常粗略的估计,请务必按照您的方式进行。只是不要对结果过于相信,因为它们与你所谓的“科学”相去甚远
In essence: No.
Benchmarking is a very difficult problem which usually is not worth solving yourself. It all depends on why you care. Your method will surely give a very rough estimate on if System A is better than System B, but really only when the outcome is vastly different.
What you're trying to do is determine how Real World Application X will perform on different computers. Very rarely is a real world application approximated by a loop of simple math. Even when it is (scientific computing mostly) you're better off measuring times on the actual program.
Real world applications are usually non-linear, and difficult to measure and simulate. Its really one of those problems which has been solved by someone else much better than you could reasonably solve yourself.
If you want a very rough estimate of performance, sure do it your way. Just don't put too much faith in the results because they will be far from what you might call "scientific"
如果我正确理解你的意图(你可以澄清一下 - 你试图测量或估计的到底是什么,处理器速度,代码速度,其他东西以及目的,但如果我理解你的话)为什么不呢检查 timeit 是如何完成的
If I understand your intention correctly (and you could clarify it a bit - what is it exactly that you are trying to measure or estimate, processor speed, code speed, something else and for what purpose, but if I understand you then) why not check how is it done in timeit