Is Python Bitwise Shift Really Slow?
Solution 1:
Just trying to reply directly to the question you posed. It is not a simple question to answer why Python takes 9 seconds and Java is 50 times faster. Here you can get a good insight of a precedent discussion Is Python slower than Java/C#?
The way I like to look at it, is that Java is a Object Oriented language, while python is Object Based.
When looking at a bitwise operation, Java uses the primitive data types that are arguably faster due to not having boxing-unboxing operations and wrappers as a layer of abstraction. So looking at your code at every iteration python re-wrappes the integer as an object of type integer, while Java does not.
But again, I wouldn't take for granted that Java is always faster than Python. It is up to which library you are using and which problem you are trying to solve!
Solution 2:
The pythonic way to do this is
defsolution(array):
returnlen(set(array))
It is much faster, though will probably use more memory.
The set
solution ran in about 100 ms for 10**6
samples from a 2*10**6
range. I didn't even time the bit array because it took seconds.
When talking about lists on the order of 10**6
, it is worth the trade off. Using sys.getsizeof
, I measured the intermediate set
as using 4.2 times the memory of the list
. The equivalent int
bit array has about 1/30 the memory of the list
. This is on a 64 bit Linux system.
Post a Comment for "Is Python Bitwise Shift Really Slow?"