Created
November 27, 2012 02:09
-
-
Save bradleyayers/4151954 to your computer and use it in GitHub Desktop.
Comparison of item removal techniques in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import numpy.ma | |
import random | |
import timeit | |
def using_del(numbers, indices): | |
numbers = numbers[:] | |
offset = 0 | |
for i in indices: | |
del numbers[i - offset] | |
offset += 1 | |
def using_set(numbers, indices): | |
indices = set(indices) | |
[x for i, x in enumerate(numbers) if i not in indices] | |
def using_list(numbers, indices): | |
[x for i, x in enumerate(numbers) if i not in indices] | |
def using_numpy(numbers, indices): | |
a = numpy.array(numbers) | |
numpy.delete(a, indices) | |
def using_slices(numbers, indices): | |
result = [] | |
i = 0 | |
for j in indices: | |
result += numbers[i:j] | |
i = j + 1 | |
result += numbers[i:] | |
def using_masked(numbers, indices): | |
numbers = numpy.ma.array(numbers) | |
numbers[indices] = numpy.ma.masked | |
for i in range(15, 25, 1): | |
size = 2 ** i | |
tests = [ | |
using_list, | |
using_del, | |
using_numpy, | |
using_set, | |
using_slices, | |
using_masked, | |
] | |
results = [] | |
for test in tests: | |
numbers = range(size) | |
# make 1/20 the number of indices | |
indices = sorted(set([random.randrange(0, size) for _ in xrange(size / 20)])) | |
results.append(timeit.timeit(lambda: test(numbers, indices), number=1)) | |
print '2^%d' % i, '\t', '\t'.join(map(str, results)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment