简体   繁体   English

测量 Python 和 Java 实现之间的性能差异?

[英]Measuring performance difference between Python and Java implementations?

I want to compare the performance of two different implementations (Python and Java) of the same algorithm.我想比较同一算法的两种不同实现(Python 和 Java)的性能。 I run scripts on the terminal (using Ubuntu 18) like this:我在终端上运行脚本(使用 Ubuntu 18),如下所示:

time script_name

I'm not sure how accurate this is.我不确定这是多么准确。 Is it possible to increase the accuracy of this benchmark?是否可以提高此基准测试的准确性? Perhaps there is a way to remove any restrictions or set-up in Python or Java?也许有一种方法可以消除 Python 或 Java 中的任何限制或设置?

The accuracy of the time command is probably fine for most testing.对于大多数测试来说, time命令的准确性可能很好。

But if you wanted, you could write a 2nd Python script for timing with an import subprocess that then uses the subprocess.call() function (or one of several related functions in the subprocess module) to run the two versions of the algorithm.但是,如果您愿意,您可以编写第二个 Python 脚本,用于使用import subprocess计时,然后使用subprocess.call()函数(或subprocess模块中的几个相关函数之一)来运行算法的两个版本。

Your timing script could also import time and then do datetime.datetime.now().time() before and after the algorithm runs, to show how much time passed.您的计时脚本也可以import time ,然后在算法运行前后执行datetime.datetime.now().time()以显示经过了多少时间。

As explained in this answer , the correct way to benchmark a program using time is the following command: sudo chrt -f 99 /usr/bin/time --verbose <benchmark> .本答案所述,使用time对程序进行基准测试的正确方法是以下命令: sudo chrt -f 99 /usr/bin/time --verbose <benchmark> However, note that this will only be accurate if the algorithm takes at least a second to execute, as otherwise the exec call might take up a big part of the benchmark.但是,请注意,只有当算法至少需要一秒钟才能执行时,这才是准确的,否则exec调用可能会占用基准测试的很大一部分。

This answer suggests using perf stat instead, as such: perf stat -r 10 -d <your app and arguments> .此答案建议改用perf stat ,例如: perf stat -r 10 -d <your app and arguments>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM