Skip to content

Instantly share code, notes, and snippets.

@chew-z
Last active August 29, 2015 14:16
Show Gist options
  • Save chew-z/fec1d3ef89e1618f96e5 to your computer and use it in GitHub Desktop.
Save chew-z/fec1d3ef89e1618f96e5 to your computer and use it in GitHub Desktop.
#http://stackoverflow.com/questions/28252442/speeding-up-this-python-code-for-large-input/28261161#28261161
from bitarray import BitArray
from random import randint
import numpy as np
def SplitArray(l, r):
while(l < r):
k = randint(l, r)
arr.set_bit(k)
return SplitArray(l, k-2) + [k] + SplitArray(k+2, r)
return []
def count0(ba):
i = 0
for n in xrange(1, N):
if ba.get_bit(n) == 0:
i += 1
return i
N = 50000
L = 1000
dist = np.zeros(L)
for i in xrange(L):
arr = BitArray(N, initialize = 0)
SplitArray(1, N)
dist[i] = count0(arr)
print np.mean(dist), np.std(dist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment