I know this is similar to How do I pick 2 random items from a Python set?
say you have iterable s, random.sample(s, n) probably does the job.
is the run time O(|s|) or O(n) ?
# my investigation Code
import random, time
a = range(100)
c = range(25)
b = map(lambda x: (1, x), a)
print a
print b
N = 1000000
def sampleIterable(items, n):
t1 = time.clock()
for _ in xrange(N):
random.sample(items, n)
t2 = time.clock()
print "Time Elapsed = %s sec" % (t2 - t1)
print
print "====== sampling ======"
sampleIterable(a, 5)
sampleIterable(b, 5)
sampleIterable(c, 5)
print "-----------"
sampleIterable(a, 20)
sampleIterable(b, 20)
sampleIterable(c, 20)
print "-----------"
sampleIterable(set(a), 20)
sampleIterable(set(b), 20)
sampleIterable(set(c), 20)
Result: ====== sampling ====== Time Elapsed = 4.014588 sec
Time Elapsed = 3.56045 sec
Time Elapsed = 3.619019 sec
Time Elapsed = 10.594099 sec
Time Elapsed = 10.277008 sec
Time Elapsed = 9.707444 sec
Time Elapsed = 19.782511 sec
Time Elapsed = 19.71995 sec
Time Elapsed = 10.212379 sec
Guess the conclusion is: yes it's O(n) not O(|S|) and sampling from list is faster than set.
In general how do I look up the run time of various python method though?