java中的math pow,Java Math.pow(a,b)时间复杂度

博主分析了Java Math.pow()函数的时间复杂性,指出在一般情况下是O(1),特别提到了OpenJDK实现的两种策略:幂运算系列法和C标准库调用。结论是,除非特殊情况,通常认为其为常数时间复杂度。
摘要由CSDN通过智能技术生成

I would like to ask time complexity of the following code. Is it O(n)? (Is time complexity of Math.pow() O(1)? ) In general, is Math.pow(a,b) has time complexity O(b) or O(1)? Thanks in advance.

public void foo(int[] ar) {

int n = ar.length;

int sum = 0;

for(int i = 0; i < n; ++i) {

sum += Math.pow(10,ar[i]);

}

}

解决方案

@Blindy talks about possible approaches that Java could take in implementing pow.

First of all, the general case cannot be repeated multiplication. It won't work for the general case where the exponent is not an integer. (The signature for pow is Math.pow(double, double)!)

In the OpenJDK 8 codebase, the native code implementation for pow can work in two ways:

The first implementation in e_pow.c uses a power series. The approach is described in the C comments as follows:

* Method: Let x = 2 * (1+f)

* 1. Compute and return log2(x) in two pieces:

* log2(x) = w1 + w2,

* where w1 has 53-24 = 29 bit trailing zeros.

* 2. Perform y*log2(x) = n+y' by simulating multi-precision

* arithmetic, where |y'|<=0.5.

* 3. Return x**y = 2**n*exp(y'*log2)

The second implementation in w_pow.c is a wrapper for the pow function provided by the Standard C library. The wrapper deals with edge cases.

Now it is possible that the Standard C library uses CPU specific math instructions. If it did, and the JDK build (or runtime) selected1 the second implementation, then Java would use those instructions too.

But either way, I can see no trace of any special case code that uses repeated multiplication. You can safely assume that it is O(1).

1 - I haven't delved into how when the selection is / can be made.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值