Python, for loop, map function, list comprehension efficiency comparison

Source: Internet
Author: User

In our usual code, we will certainly encounter a number of conversions from one list to another to give an example of each int element +1 in the list, usually we will use 3 different ways:

Array = range (+)#  loop a = [] for in array:    a.append (i +1)#map function a = map (Lambda x:x+1, array)# list derivation   for in array]

What is the difference between the above three kinds of writing, which is the best way to read Google's code specification said the third kind of list deduction, then why the recommendation list deduction?

We use Timeit for a simple evaluation in Ipython:

#LoopsArray=range (1000) A=[]%timeit forIinchArray:a.append (i+1)#loops, Best of 3:156 us per loop#map Function%timeit Map (LambdaX:x+1, Array)#10000 loops, best of 3:172 us per loop#List Derivation%timeit [x+1 forXinchArray]#10000 loops, best of 3:68.7 us per loop

You can see that the advantages of list deduction are very obvious.

Why is this happening? We use the DIS module to see which underlying resources are called by each method

deftest_for (Array): a= []     forIinchArray:a.append (i+1)    returnAdis.dis (test_for)20 build_list 03 Store_fast 1(a)3 6 Setup_loop (to 40)              9load_fast 0 (array)12Get_iter>> For_iter (to 39)             2 Store_fast(i)4 Load_fast 1(a)22load_attr 0 (Append)2 Load_fast(i)Load_const 1 (1)             31Binary_addCall_function 1 35Pop_topJump_absolute >> 39Pop_block5 >> Load_fast 1(a)Return_value

Can be seen in the For loop, in the main loop body, the program repeatedly calls load and call

def Test_map (Array):     return map (Lambda x:x+1, array) Dis.dis (test_map)  2           0 load_global              0 (map)               3 load_const               1 (<code object <lambda"< ipython-input-20-4aa500644b58>", line 2>)              6 make_function            0               9 load_fast                0 (array)             call_function            2             return_value

The map loop constructs an anonymous function and calls the function call with map

deftest_list (array):return[X+1 forXinchArray]dis.dis (test_list):20 build_list 03load_fast 0 (array)6Get_iter>> 7 For_iter (to 26)             Ten Store_fast 1(x)1 load_fast(x)1 Load_const (1)             19Binary_addList_append 2 Jump_absolute 7 >> Return_value

List derivation actually used a thing like list_append to record the results.

We all know that it's faster to invoke the underlying, so it's quicker to use a list derivation because he doesn't call other functions

So how do you modify the first two methods to make them faster?

1,for Loop, we note that there are two steps in the For loop, one is load, but call, and if the load process is recorded, then the speed will be faster

A == a.append for in Array:test_func (i+1)#10000 loops, best of 3:100 US per loop

Compared to the previous wording, there is a significant increase

2,map function, we used our custom lambda anonymous function in the first test, and if we set the anonymous function as the underlying simple addition, the speed would be significantly increased.

Int_obj=1%timeit map (int_obj.__add__, array)#10000 loops, best of 3:67.6 US per loop
    

We were surprised to find that its speed was almost the same as the list derivation

Then there's the question: Why is there a list derivation and for? If for a complex conversion operation, the efficiency of the list deduction is actually the same as for the for.

def Add (x):     return  for inch Array] # loops, Best of 3:180 us per loop

As always: simple cyclic mapping operations, we recommend the form of a list deduction, which is more efficient and faster. Complex cyclic mapping operations, we recommend using a For loop, which makes the code easier to read and understand. And for the map method, we think this is an outdated way of writing, should be used sparingly, not even.

Reference: https://www.zhihu.com/question/34637934

Results measured above: Python 2.7.3,ipython 0.12.1

Python, for loop, map function, list comprehension efficiency comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.