小编Bri*_*ian的帖子

Java中的"快速"整数权力

[简答:糟糕的基准测试方法.你觉得我现在已经想到了.]

问题表现为"找到一种快速计算x ^ y的方法,其中x和y是正整数".典型的"快速"算法如下所示:

public long fastPower(int x, int y) {
  // Replaced my code with the "better" version described below,
  // but this version isn't measurably faster than what I had before
  long base = x; // otherwise, we may overflow at x *= x.
  long result = y % 2 == 1 ? x : 1;
  while (y > 1) {
    base *= base;
    y >>= 1;
    if (y % 2 == 1) result *= base;
  }

  return …
Run Code Online (Sandbox Code Playgroud)

java algorithm performance

6
推荐指数
1
解决办法
1804
查看次数

标签 统计

algorithm ×1

java ×1

performance ×1