I must be overlooking something, but really don't see why the Python code is so slow...
Counting unique elements in an array where elements are in the range [−1,000,000..1,000,000] and use a bitvector to do this. The Java code, which uses BitSet is about 50 times faster than Python, which takes 9 seconds.
Is this maybe because when I initialise bitvector = 0 Python doesn't reserve enough memory and the bitvector needs to be copied as it grows?
Python:
def solution(array):
bitvector = 0
count = 0
for element in array:
# transform -1,000,000 to 0 etc
element_transformed = element + 1000000
if bitvector >> element_transformed & 1 == 0:
count += 1
bitvector = bitvector | 1 << element_transformed
return count
Test:
import unittest
import random
from .file1 import solution
class MySolutionTests(unittest.TestCase):
def test_solution_random_all_unique(self):
a = random.sample(range(-1000000, 1000001), 100000)
self.assertEqual(100000, solution(a))
In Java:
package mypackage;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
public class MyClass {
public static int solution(List<Integer> array) {
BitSet bitvector = new BitSet();
int count = 0;
for(int i = 0; i < array.size(); i++) {
int elementTransformed = array.get(i) + 1000000;
if(bitvector.get(elementTransformed) != true) {
count++;
bitvector.set(elementTransformed, true);
}
}
return count;
}
public static void main(String[] args) {
// TODO code application logic here
}
}
Test:
package mypackage;
import java.util.ArrayList;
import java.util.Collections;
import org.junit.Test;
import static org.junit.Assert.*;
public class MyClassTest {
public MyClassTest() {
}
@Test
public void testSolutionLong_RandomAllUnique() {
ArrayList array = new ArrayList();
for(int i = -1000000; i < 1000000; i++) {
array.add(i);
}
Collections.shuffle(array);
assertEquals(100000, MyClass.solution(array.subList(0, 100000)));
}
}