如何用Java编写(并运行)正确的微基准测试?
我正在寻找一些代码示例和注释,以说明需要考虑的各种事情。
示例:基准测试应该测量时间/迭代还是迭代/时间,为什么?
相关:秒表基准测试是可接受的?
如何用Java编写(并运行)正确的微基准测试?
我正在寻找一些代码示例和注释,以说明需要考虑的各种事情。
示例:基准测试应该测量时间/迭代还是迭代/时间,为什么?
相关:秒表基准测试是可接受的?
当前回答
如果您正在尝试比较两种算法,那么每种算法至少要进行两次基准测试,交替使用顺序。例如:
for(i=1..n)
alg1();
for(i=1..n)
alg2();
for(i=1..n)
alg2();
for(i=1..n)
alg1();
我发现了一些明显的差异(有时5-10%)在运行时相同的算法在不同的通行证。
此外,还要确保n非常大,以便每个循环的运行时间至少为10秒左右。迭代次数越多,基准测试时间中的数字就越重要,数据就越可靠。
其他回答
还应该注意的是,在比较不同的实现时,分析微基准测试的结果可能也很重要。因此,应进行显著性检验。
这是因为在基准测试的大多数运行过程中,实现A可能比实现B更快,但A也可能有更高的差异,因此与B相比,A的测量性能收益将没有任何意义。
因此正确编写和运行微基准测试也很重要,但正确分析它也很重要。
我知道这个问题已经被标记为已回答,但我想提到两个帮助我们编写微基准的库
来自谷歌的卡尺
入门教程
http://codingjunkie.net/micro-benchmarking-with-caliper/ http://vertexlabs.co.uk/blog/caliper
来自OpenJDK的JMH
入门教程
避免JVM上的基准测试陷阱 使用JMH进行Java微基准测试 JMH简介
如果您正在尝试比较两种算法,那么每种算法至少要进行两次基准测试,交替使用顺序。例如:
for(i=1..n)
alg1();
for(i=1..n)
alg2();
for(i=1..n)
alg2();
for(i=1..n)
alg1();
我发现了一些明显的差异(有时5-10%)在运行时相同的算法在不同的通行证。
此外,还要确保n非常大,以便每个循环的运行时间至少为10秒左右。迭代次数越多,基准测试时间中的数字就越重要,数据就越可靠。
确保以某种方式使用在基准代码中计算的结果。否则你的代码会被优化掉。
Java基准测试的重要事项是:
Warm up the JIT first by running the code several times before timing it Make sure you run it for long enough to be able to measure the results in seconds or (better) tens of seconds While you can't call System.gc() between iterations, it's a good idea to run it between tests, so that each test will hopefully get a "clean" memory space to work with. (Yes, gc() is more of a hint than a guarantee, but it's very likely that it really will garbage collect in my experience.) I like to display iterations and time, and a score of time/iteration which can be scaled such that the "best" algorithm gets a score of 1.0 and others are scored in a relative fashion. This means you can run all algorithms for a longish time, varying both number of iterations and time, but still getting comparable results.
我正在写一篇关于。net基准测试框架设计的博客。我有一些以前的帖子,也许能给你一些想法——当然,不是每件事都合适,但其中一些可能是合适的。