如何禁用编译器和 JVM 优化? [英] How to disable compiler and JVM optimizations?

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

问题描述

我有这段代码正在测试 Calendar.getInstance().getTimeInMillis()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 the difference.

如何确定?

EDIT:我更改了代码示例,使其更加清晰.我在这里检查的是在不同的实现中调用 getTimeInMilli() 需要多少时间 - Calendar vs System.

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天全站免登陆