python - Optimizing operations on lists -


I need to process a lot of data in the lists and by doing so, it is seeing that the best The way Python is using

The main ways I am using: - Understanding the list - Generator expressions - Functional style operation (map, filter etc.)

Know that the probability of the list is usually the most "Pythonic" method, but What is not the case the best? Inspired by this answer: I have touched the questions to compare generator expressions:

/ P>

For built-in:

  $ python -mtimeit -s' import math; Xs = range (10) '' yoga (map (math.sqrt, xs) '100000 loops, best 3: 2.96 per pipe per python- $ python -mtimeit -s' import math; Xs = range (10) 'sum' (xs for [math .sqrt x] xs] '100000 loops, best 3: 3.75 per loop $ python-meet time-s' import math; Xs = range (10) '' x '' in x for math.sqrt (x) 100,000 loop, best 3: 3.71 per loop   <10> (10) '' Yoga (Map (Lambda X: X + 2, XS)) '100000 loops, best 3: 2.98 per use loop $ Python- amityime' x 'exchange (range) (10) "' yoga ([x + 2x For X]] 100,000 loops, use the best 3: 1.66 pipes $ Python -may-time -s 'xs = range (10)' 'x (x in x for x + x)' 100000 loops, Use best 3: 1.48 per loop   

create a list:

  $ python - MTITIT -sx = range (10) "list (map (lambda x: x + 2, xs)) '100000 loops, use best 3: 3.1 9 pipes per $ $ python -mtimeit -s' xs = grade (10) '' [x x for x + x] '' 100000 loops, best 3: 1.21 Payeth per five python -may-time x category = (10) "list (x in x for 2 + x) '100000 loop, best use 3: 3.36 per loop  

seem to be Otherwise, if added to the built code, otherwise the map generator defeats the understanding of expression list. Along with a few cleaner syntax, Generator Expressions also save more memory on the understanding of the list because they evaluate lightly. Therefore, in the absence of specific tests for your application, you should use the map with Biltions, the list list is required when you need the list result, otherwise the generator will display if you actually Concerned about, you can see if you really need lists at all points of your program.


Comments