Created
June 20, 2013 16:16
-
-
Save mpaolino/5824219 to your computer and use it in GitHub Desktop.
Heavy python list slicing _will_ fuck efficiency, if applicable consider numpy arrays to do the job
This file contains hidden or 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
mpaolino@guacamole:~$ cat bench_slice.py | |
import numpy | |
import timeit | |
def splitit(arraylist): | |
step = 10 | |
frame = 160 | |
while len(arraylist) >= frame: | |
newlist = arraylist[:frame] | |
arraylist = arraylist[step:] | |
for i in [200, 500, 1000, 10000, 100000]: | |
print '-'*80 | |
print 'N =', i | |
nparray = numpy.linspace(0, 1, i) | |
pylist = nparray.tolist() | |
print "List:", timeit.timeit(stmt='splitit(pylist)', | |
setup='from __main__ import splitit, nparray, pylist', | |
number=10) | |
print "NP array:", timeit.timeit(stmt='splitit(nparray)', | |
setup='from __main__ import splitit, nparray, pylist', | |
number=10) | |
mpaolino@guacamole:~$ python bench_slice.py | |
-------------------------------------------------------------------------------- | |
N = 200 | |
List: 0.000123023986816 | |
NP array: 7.41481781006e-05 | |
-------------------------------------------------------------------------------- | |
N = 500 | |
List: 0.000946044921875 | |
NP array: 0.000421047210693 | |
-------------------------------------------------------------------------------- | |
N = 1000 | |
List: 0.00359988212585 | |
NP array: 0.000972986221313 | |
-------------------------------------------------------------------------------- | |
N = 10000 | |
List: 0.346795797348 | |
NP array: 0.0114970207214 | |
-------------------------------------------------------------------------------- | |
N = 100000 | |
List: 153.226103067 | |
NP array: 0.133377790451 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment