I need to sample uniformly at random a number from a set with fixed size, do some calculation, and put the new number back into the set. (The number samples needed is very large)
I've tried to store the numbers in a list and use random.choice() to pick an element, remove it, and then append the new element. But that's way too slow!
I'm thinking to store the numbers in a numpy array, sample a list of indices, and for each index perform the calculation.
- Are there any faster way of doing this process?
Python lists are implemented internally as arrays (like Java
ArrayList
s, C++std::vector
s, etc.), so removing an element from the middle is relatively slow: all subsequent elements have to be reindexed. (See http://www.laurentluce.com/posts/python-list-implementation/ for more on this.) Since the order of elements doesn't seem to be relevant to you, I'd recommend you just userandom.randint(0, len(L) - 1)
to choose an indexi
, then useL[i] = calculation(L[i])
to update thei
th element.random.sample( a set or list or Numpy array, Nsample ) is very fast, but it's not clear to me if you want anything like this:
You could use Numpy arrays or bitarray instead of
set
, but I'd expect the time in calc() to dominate.What are your Setsize and Samplesize, roughly ?