What is the Big O of bit count? I'm not sure how the method works, but I would assume it is done in O(logn).
Specifically with this code (where x = 4, y = 1):
return Integer.bitCount(x^y);
What is the Big O of bit count? I'm not sure how the method works, but I would assume it is done in O(logn).
Specifically with this code (where x = 4, y = 1):
return Integer.bitCount(x^y);
Given its implementation, the method consists of six O(1) statements performed in sequence, so it's O(1).
public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
popcnt
instruction on platforms that support it (most of the interesting ones). Doesn't really change the conclusion though (although it does mean that Integer.bitCount()
and Long.bitCount()
usually take the same time - which wouldn't be the your conclusion if you looked at the source. –
Darkle It is O(1)
, here is its implementation for JDK 1.5+:
public static int bitCount(int i) {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
Integer.bitCount
and several other simple methods in the Integer
, Long
, etc classes are implemented as JVM intrinsics which usually result in a single popcnt
type instruction being emitted. Still O(1) though :) –
Darkle do_intrinsic(_bitCount_i, java_lang_Integer, bitCount_name, int_int_signature, F_S)
which indicates that the function is potentially an intrinsic. –
Darkle .cpp
source to see if it actually is on the platform of interest (usually it is once you find it in the .hpp
but it may not be for several reasons such as the lack of support on a particular platform, etc). In the case of bitCount
it usually ends up being an instrinsic on modern platforms of interest. Another approach is just to execute the method in question in a hot loop and then use something like perf top
to check out the instruction(s) being executed. See also this question. –
Darkle Integer.bitCount()
I get ~2.0 billion calls to bitCount
per second, while for an exact copy of the JDK code I get only 0.4 billion calls/second. –
Darkle memcpy
and replace it, often. –
Darkle native
methods are very different. This is part of the JVM spec (probably the native
keyword is briefly mentioned in the JLS as well) for implementing JNI methods. Basically you have a C-like function in some binary, and that function is called and does its work and returns. There are rules for how parameters are passed and various other things. It should work on any platform, etc - although of course you need to include different libraries for different architectures. –
Darkle popcnt
assembly instruction, use it when you are compiling code that uses Integer.bitCount
. There is effectively zero overhead, while native
calls have an overhead of 20 cycles or more, usually. @Rubbing –
Darkle popcnt
bytecode, but by telling the JIT about the special methods in the Integer
class it's almost like there was. Some methods pretty much have to be implemented as an intrinsic, like the methods in the Atomic*
classes: those methods have the native
keyword but they are actually instrinsics under the covers... –
Darkle Any algorithm that work on input of limited size have complexity of O(1)
.
bitCount
works with input of limited size.
Therefore bitCount
have complexity of O(1)
.
© 2022 - 2024 — McMap. All rights reserved.
n
in yourO(log n)
? The total number of bits in the integer? When talking about Big O we set some arbitrary operation to have cost 1, and it's fair to assumebitCount
has cost 1. – PirandelloO(1)
, see e.g. #1458814 – Garnierite