如何确保没有jvm和编译器优化发生 [英] how to make sure no jvm and compiler optimization occurs

查看:196
本文介绍了如何确保没有jvm和编译器优化发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码测试 Calendar.getInstance()。getTimeInMillis() vs System.currentTimeMilli()

I have this code that is testing Calendar.getInstance().getTimeInMillis() vs System.currentTimeMilli() :

long before = getTimeInMilli();
for (int i = 0; i < TIMES_TO_ITERATE; i++)
{
  long before1 = getTimeInMilli();
  doSomeReallyHardWork();
  long after1 = getTimeInMilli();
}
long after = getTimeInMilli();
System.out.println(getClass().getSimpleName() + " total is " + (after - before));

我想确保没有JVM或编译器优化发生,所以测试将是有效的,显示差异。

I want to make sure no JVM or compiler optimization happens, so the test will be valid and will actually show difference.

如何确定?

EDIT 代码示例所以会更清楚。我在这里检查是在不同的实现中调用getTimeInMilli()需要多少时间 - 日历vs系统。

EDIT: I changed the code example so it will be more clear. What I am checking here is how much time it takes to call getTimeInMilli() in different implementations - Calendar vs System.

推荐答案

认为你需要禁用JIT。添加到您的运行命令下一个选项:

I think you need to disable JIT. Add to your run command next option:

-Djava.compiler=NONE

这篇关于如何确保没有jvm和编译器优化发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆