java - 运行 Java,无需进行数组边界检查、强制转换检查等

标签 java optimization profiling

我有一个 Java 程序,可以执行许多小型数组操作。我已经运行它并验证它不会产生错误,但它比预期慢。我假设这种减速很大程度上与检查数组边界等有关。

有没有办法禁用所有错误检查,以便我可以更快地运行我的程序(冒着崩溃的风险,而不是生成可理解的错误)?

最佳答案

不,这是不可能的。 Java 语言规范描述了强制转换和数组访问抛出异常的情况,但没有提及禁用此行为的机制。

ClassCastException 的抛出在 JLS Section 15.6 中描述。 :

  • A cast expression (§15.16) throws a ClassCastException if a cast is found to be impermissible at run time.

数组边界检查在JLS Section 15.10.4中描述。 :

At run time, evaluation of an array access expression behaves as follows:

  • First, the array reference expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason and the index expression is not evaluated.

  • Otherwise, the index expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason.

  • Otherwise, if the value of the array reference expression is null, then a NullPointerException is thrown.

  • Otherwise, the value of the array reference expression indeed refers to an array. If the value of the index expression is less than zero, or greater than or equal to the array's length, then an ArrayIndexOutOfBoundsException is thrown.

  • Otherwise, the result of the array access is the variable of type T, within the array, selected by the value of the index expression.

关于java - 运行 Java,无需进行数组边界检查、强制转换检查等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37209650/

相关文章:

performance - 人们可以使用分析器,但为什么不直接停止程序呢?

java - Netbeans JDK 校准 8.1

c# - .NET 有 “function size profiler” 吗?

java - 从 selenium webdriver 中的消息中获取特定值

java - jar文件无法加载本体,而eclipse中的java代码加载它

optimization - 最大化整数和的组合数

optimization - 神经网络优化

r - 等同于min()的rowMeans()

java - 我是不是误解了 Java Bean 方法命名约定,或者这是一种异常情况?

java - 如何在 spring boot 到达 Controller 之前修改请求体