Skip to content

JEP 177: Optimize java.text.DecimalFormat.format | 优化 java.text.DecimalFormat.format

摘要

通过利用整数和浮点算术的数字属性来优化 java.text.DecimalFormat.format,以加速小数点后有两到三位数字的情况。

目标

使 DecimalFormat 的常用操作更快。

成功标准

在感兴趣的微基准测试中,至少达到 2 倍的速度提升。

描述

最关注的十进制格式转换是那些小数点后有两到三位数字的情况。与其执行昂贵的浮点除法来隔离和舍入小数位,不如对小数部分进行 100.0 或 1000.0 的浮点乘法,将乘积转换为整数值,然后将得到的整数转换为十进制数。虽然这个过程更快,但必须注意避免双重舍入和其他数值风险。

利用哪些小数二进制值可以精确表示的属性进行案例分析,将乘法后的舍入减少为查找表风格的计算。

测试

除了运行现有的 JCK 和回归测试外,还将开发针对优化代码路径边界情况的新测试。此外,还将在当代硬件平台上使用微基准测试来评估代码的性能。

风险和假设

使用优化实现进行的探索性工作揭示了 DecimalFormat 中存在的一个长期存在的数值错误:一些接近中间值的案例没有正确舍入。Java SE 8 正在修改 DecimalFormat 的规范,以明确要求始终进行正确的舍入。

影响

  • 兼容性:在 JDK 7 中,(正确的)但已改变的数值行为将仅在启用了激进的优化标志的情况下启用,以限制在该发布序列中的行为兼容性影响。在 Java SE 8 中,正确的数值行为将由规范始终要求。

  • 性能 / 可扩展性:许多常见情况将因新算法而更快;其他情况将回退到使用旧代码。

  • TCK:JCK 8 中的一些新测试用例将会有所帮助。

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.