python - What's faster: to reorder a list of lists or to reorder a list of indexes? -


what faster: reorder list of lists or reorder list of integers?

should first represent main list list of indexes (to represent list of ordinal type) of inner lists or can work directly list of lists?

from test, can see take same amount of time:

a = range(100) b = [range(10) _ in range(100)]  if __name__ == '__main__':     timeit import repeat     n = 500     r = 5      print min(repeat('shuffle(a)', setup='from random import shuffle;from __main__ import a', repeat=r, number=n))     print min(repeat('shuffle(b)', setup='from random import shuffle;from __main__ import b', repeat=r, number=n)) 

i noticed slight speed advantage reordering list of ints, it's neither significant, nor consistent enough warrant using list of indices. sometimes, reordering lists of lists faster , vice versa.

this because objects binded reference values stored , passed around variables, , everything object in python, int types. "reordering lists within list" equivalent "reordering numbers within list" because you're really doing reordering list of references lists/numbers respectively.

you can think of reference value sort of address in memory points object located. in python terms, it's id of object. it's analogous pointer value in c/c++. in cpython, id of object it's address in memory, implementation detail, don't count on being same across python implementations.


Popular posts from this blog