简体   繁体   English

Java中Math.abs的时间复杂度?

[英]Time complexity of Math.abs in Java?

For an assignment I'm doing, I need to make sure I have an algorithm running in O(n) time, and I'm using the Math.abs() function inside some loops, so I'm wondering if Math.abs() runs in O(1) time? 对于我正在做的作业,我需要确保我有一个算法在O(n)时间内运行,并且在某些循环中使用了Math.abs()函数,因此我想知道Math.abs()在O(1)时间运行?

I would think that it would, but I can't find an answer to this anywhere. 我认为可以,但是我在任何地方都找不到答案。 Just want to make sure I'm not accidentally making an O(n 2 ) algorithm without knowing it. 只是想确保我不会在不知道的情况下意外地创建O(n 2 )算法。

The Math.abs implementation: Math.abs实现:

public static int abs(int a) {
    return (a < 0) ? -a : a;
}

This will be O(1) time complexity, since the operation itself is constant time and the input is fixed. 这将是O(1)时间复杂度,因为操作本身是恒定时间并且输入是固定的。 Regardless of the input, the operation will always take the save time. 无论输入什么,该操作将始终节省时间。

Loop: O(n): Time Complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. 循环: O(n):如果循环变量递增/递减恒定量,则循环的时间复杂度被视为O(n)。 For example following functions have O(n) time complexity. 例如,以下函数的时间复杂度为O(n)。

   // Here c is a positive integer constant   
   for (int i = 1; i <= n; i += c) {  
        // some O(1) expressions
   }

   for (int i = n; i > 0; i -= c) {
        // some O(1) expressions
   }

That depends on what data type you performing the abs function: 这取决于您执行abs函数的数据类型:

  1. IEEE754 floating point IEEE754浮点

    abs means just clearing of MSB bit which holds the mantissa sign bit. abs表示只清除保留尾数符号位的MSB位。 This is unconditional bit operation on single well placed bit so it is O(1) . 这是对单个放置好的位的无条件位操作,因此为O(1) Here C++ example (sorry not a JAVA programmer) 这里是C ++示例(抱歉,不是JAVA程序员)

     float x; // 32bit variable to perform abs on int *p=(int*)&x; // this just makes p pointer pointing to x p[0]&=0x7FFFFFFF; // clear highest bit by AND 
  2. non 2'os complement signed integer types 非2'os补码有符号整数类型

    These have separate sign bit just like previous bullet so all the stuff from #1 applies for these too. 它们与前面的项目符号一样具有单独的符号位,因此来自#1的所有内容也适用于这些。

     int x; // 32bit variable to perform abs on x&=0x7FFFFFFF; // clear highest bit by AND 
  3. 2'os complement signed integer types 2'os补码带符号整数类型

    abs for these means negate the sign if MSB is set. 如果设置了MSB,则表示为abs取反。 That means you need to negate all bits an increment. 这意味着您需要对所有位取反。

     int x; // 32bit variable to perform abs on if (int(x&0x80000000)!=0) // negative means MSB is set { x^=0xFFFFFFFF; // negate all bits x++; // increment } 

    This can be done also brunch less. 这也可以减少早午餐。 Anyway for basic data types is this still O(1) . 无论如何,对于基本数据类型仍然是O(1) But if you start using bigint or bigdecimal then both negation of all bits and incrementation will become O(n) where n is the number of "digits" used to form the number. 但是,如果您开始使用bigintbigdecimal则所有位的bigdecimal反和增量都将变为O(n) ,其中n是用于形成数字的“数字”数。 By the "Digit" I mean what base is used to internally store the number. “数字”是指内部用来存储数字的基数。 usually it is 2^32 or 2^64 word or highest power of 10 that can be fit inside such number. 通常是2^322^64字或10最高幂可以适合该数字。 That is why is usually better not to use 2'os complement for big numbers ... (it complicates thing not just abs operation). 这就是为什么通常最好不要对大数使用2'os补码...(这会使事情变得复杂,而不仅仅是abs操作)。

Conclusion 结论

On basic data types you can safely assume abs is O(1) and also most HW architectures support this operation as an atomic instruction. 在基本数据类型上,您可以放心地假设absO(1) ,而且大多数HW体系结构都支持此操作作为原子指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM