2x speedup in python code