获得10的力量的最快方法

Xer*_*rus 2 java math pow

我将在这里使用Java作为示例,因为它可能完全取决于语言,但我通常对如何做到这一点感兴趣.
当然,最简单明了的方法是:

Math.pow(10, x)
Run Code Online (Sandbox Code Playgroud)

但据我所知,电力是一项相当昂贵的操作.因此我认为应该有更好的方法,因为它看起来很明显 - 我只需要1之后的x零!

编辑: 是的,这可能是过早优化.上下文是对n个小数位的舍入操作,通常使用如下所示的内容:

private final static int[] powersOf10 = {1, 10, 100, 1000, 10000};
public static double round(double number, int decimals) {
    if(decimals < 5)
        return Math.rint(number / powersOf10[decimals]) * powersOf10[decimals];
    double c = Math.pow(10, decimals);
    return Math.rint(number * c) / c;
}
Run Code Online (Sandbox Code Playgroud)

这是我使用的当前舍入函数,因为你可以看到我已经使用查找表来查找最常用的值,但我仍然很好奇是否有一些魔法或类似的东西来改善它.

Lou*_*man 15

最快的方法是

static final int[] POWERS_OF_10 = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
static int powerOfTen(int pow) {
  return POWERS_OF_10[pow];
}
Run Code Online (Sandbox Code Playgroud)

...因为没有更高的10的力量适合int.对不起,如果你期待更酷的东西.

  • 如果你想获得更高的10倍功率,请使用'long'类型...最大值应为1,000,000,000,000,000,000 (3认同)

Wil*_*ins 8

这个问题已经很老了,但是对于通过搜索到达这里的人来说,我希望这很有用。

\n

我使用 JMH 测试了三种方法的性能:

\n
    \n
  1. Math.pow(10, exponent)
  2. \n
  3. 根据接受的答案在数组中查找
  4. \n
  5. 在硬编码语句中查找switch
  6. \n
\n

这是 JMH 基准代码:

\n
private static final long[] POWERS_OF_TEN = {\n                       10L,\n                      100L,\n                    1_000L,\n                   10_000L,\n                  100_000L,\n                1_000_000L,\n               10_000_000L,\n              100_000_000L,\n            1_000_000_000L,\n           10_000_000_000L,\n          100_000_000_000L,\n        1_000_000_000_000L,\n       10_000_000_000_000L,\n      100_000_000_000_000L,\n    1_000_000_000_000_000L};\n\nint exponent;\n\n@Setup(Level.Iteration) public void randomExponent()\n{\n    exponent = RAND.nextInt(16);\n}\n\n@Benchmark public long mathPow()\n{\n    return (long) Math.pow(10, exponent);\n}\n\n@Benchmark public long arrayLookup()\n{\n    return POWERS_OF_TEN[exponent];\n}\n\n@Benchmark public long switchLookup()\n{\n    switch (exponent)\n    {\n        case 1:  { return                    10L; }\n        case 2:  { return                   100L; }\n        case 3:  { return                 1_000L; }\n        case 4:  { return                10_000L; }\n        case 5:  { return               100_000L; }\n        case 6:  { return             1_000_000L; }\n        case 7:  { return            10_000_000L; }\n        case 8:  { return           100_000_000L; }\n        case 9:  { return         1_000_000_000L; }\n        case 10: { return        10_000_000_000L; }\n        case 11: { return       100_000_000_000L; }\n        case 12: { return     1_000_000_000_000L; }\n        case 13: { return    10_000_000_000_000L; }\n        case 14: { return   100_000_000_000_000L; }\n        case 15: { return 1_000_000_000_000_000L; }\n    }\n    return 0L;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

(鼓声......)结果如下:

\n
Benchmark                           Mode  Cnt    Score    Error   Units\nPowerOfTenBenchmark.mathPow        thrpt   15   24.134 \xc2\xb1 32.132  ops/us\nPowerOfTenBenchmark.arrayLookup    thrpt   15  515.684 \xc2\xb1 10.056  ops/us\nPowerOfTenBenchmark.switchLookup   thrpt   15  461.318 \xc2\xb1  4.941  ops/us\n
Run Code Online (Sandbox Code Playgroud)\n

因此#2 数组查找是最快的,可以确认接受的答案是正确的。

\n

如果您认为POWERS_OF_TEN数组文字初始化程序很丑陋(我同意),那么有一种更短且可能不易出错的方法:

\n
Benchmark                           Mode  Cnt    Score    Error   Units\nPowerOfTenBenchmark.mathPow        thrpt   15   24.134 \xc2\xb1 32.132  ops/us\nPowerOfTenBenchmark.arrayLookup    thrpt   15  515.684 \xc2\xb1 10.056  ops/us\nPowerOfTenBenchmark.switchLookup   thrpt   15  461.318 \xc2\xb1  4.941  ops/us\n
Run Code Online (Sandbox Code Playgroud)\n