Optimization Tips for Python Code

Counters | Loops | Python Methods and Functions

This article discusses some interesting tips for optimizing Faster Python code. These methods help you get results faster in Python code.

  1. Use intrinsic functions and libraries: intrinsic functions, such as map (), are implemented in C code. Thus, the interpreter does not need to loop, this gives a significant speedup.
    The map () function applies a function to each member of the iterable and returns the result. If there are multiple arguments, map () returns a list of tuples containing matching elements from all iterations.

    # Python program to illustrate library functions
    # save time when coding with map () as an example

    import time

     
    # slower (no map ())

    start = time.clock () 

    s = 'geeks'

    U = []

    for c in s:

    U.append (c.upper ())

    print U

    elapsed = time.clock ()

    e1 = elapsed - start

    print "Time spent in function is : " , e1

      
    # Faster (Uses inline function map ())

    s = 'geeks'

    start = time.clock () 

    U = map ( str . upper, s) 

    print U

    elapsed = time.clock ()

    e2 = elapsed - start

    print "Time spent in builtin function is:" , e2

     ['G',' E', 'E',' K', 'S'] Time spent in function is: 0.0394747945637 [' G', 'E',' E', 'K',' S'] Time spent in builtin function is: 0.0212335531192 

    Packages are platform dependent, which means we need the appropriate package for the platform we are using. If we are doing a string operation, consider using an existing "collections" module such as

    # Python program for illustration
    # import container as a list with
    # quickly added and pops up at either end

    from collections import deque

    s = 'geek'

      
    # make a new deck

    d = deque (s)

     
    # add a new entry on the right side

    d.append ( 'y' )

     
    # add a new post to the left

    d.appendleft ( 'h'

    print d

     

    d.pop () # return and remove the rightmost element

     

    d.popleft () # return and remove the most left element

     
    # print list in reverse order

    print list ( reversed (d)) 

    Output:

     deque (['h',' g', 'e',' e', 'k',' y']) ['k',' e', 'e', 'g'] 

    # importing iteration tools

    import itertools

    iter = itertools.permutations ([ 1 , 2 , 3 ])

    print list ( iter )

    Output:

     [(1, 2 , 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] 
  2. Use keys for sorting: in Python we should instead use the key argument for inline sorting, which is a faster way to sort.

    # Python program for illustration
    # using sorting keys

    somelist = [ 1 , - 3 , 6 , 11 , 5 ]

    somelist.sort ()

    print somelist

     

    s = 'geeks'

    # use sorted () if you don't want to sort in place:

    s = sorted (s)

    print s

    Output:

     [-3, 1, 5, 6, 11] [' e', 'e',' g', 'k',' s'] 

    In each case, the list is sorted by the index you choose as part of the keyword argument. This approach works as well with strings as it does with numbers.

  3. Optimizing for loops: Writing idiomatic code: It may seem counterintuitive, but writing idiomatic code will make your the code is faster in most cases. This is because Python was designed to only have one obvious / correct way to accomplish a task.
    For example (String Concatenation):

    # Python program to illustrate usage
    # optimized loops for faster coding

     
    # slow O (n ^ 2) - (Note : in recent implementations it is O (n))

    s = 'hellogeeks'

    slist = ' '

    for i in s:

    slist = slist + i

    print slist

      
    # string concatenation (idiomatic and fast O (n))

    st = 'hellogeeks'  

    slist = '' .join ([i for i in s])

    print slist

      
    # Best way to iterate over the range

    evens = [i for i in xrange ( 10 ) if i % 2 = = 0 ]

    print evens

     
    # Less fast

    i = 0

    evens = [ ]

    while i & lt; 10 :

    if i % 2 = = 0 : evens.append (i)

    i + = 1

    print evens

     
    # slow

    v = 'for'

    s = 'geeks' + v + 'geeks'

    print s

     
    # fast

    s = 'geeks% s geeks' % v

    print s

     

     hellogeeks [ 0, 2, 4, 6, 8] geeks for geeks 

    Each time it loops for s (i), Python evaluates the method. However, if you put the evaluation in a variable, the value is already known and Python can perform tasks faster.

  4. Try multiple coding approaches : using exactly the same the coding approach every time you create an application will almost certainly cause the application to run slower than it could.
    For example (initializing dictionary elements):

    # Python program to illustrate attempts
    # multiple approaches encoding
    # for faster results
    # slower

    mydict = { ' g' : 1 , ' e' : 1 , 'e' : 1 , 'k' : 1 }

    word = 'pythonengineering'

    for w in word:

    if w not in mydict:

      mydict [w] = 0

    mydict [w] + = 1

    print mydict

     
    # Faster

    mydict = { 'g' : 1 , 'e' : 1 , ' e' : 1 , 'k' : 1 }

    word = 'pythonengineering'

    for w in word:

    try :

      mydict [w] + = 1

      except KeyError :

    mydict [w] = 1

    print mydict

     {'e': 5,' g': 3, 'f' : 1, 'k': 3,' o': 1, 's': 2,' r': 1} 

    The output is the same in both cases. The only difference is how the result is obtained.

  5. Use

    # slower

    x = [i for i in range ( 0 , 10 , 2 )]

    print x

     
    # Faster

    x = [i for i in xrange ( 0 , 10 , 2 )]

    print x

     [1, 3, 5, 7, 9] 

    This can save your system memory because xrange () will only produce one integer element in sequence at a time. While range (), it gives you the entire list, which does not require additional overhead to loop.

  6. Use Python multiple assignment for

    # Python program to illustrate exchange
    # variable on one line

     
    # slower

    x = 2

    y = 5

    temp = x

    x = y

    y = temp

    print x, y

     

    x, y = 3 , 5

    # Faster

    x, y = y, x

    print x, y

     5 2 5 3 
  7. If possible, use a local variable: Python is faster to retrieve a local variable than retrieve a global variable. That is, avoid the "global" keyword. Therefore, if you are going to call the method often (inside a loop), try writing it to a variable.

     0 2 


    Links:

    This article is Provided by Afzal Ansari . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

    Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





    # Python program to illustrate attempts
    # use local variables to generate code
    # run faster

    class Test:

    def func ( self , x):

    print x + x

     
    # Declaring a variable that assigns a class method object

    Obj = Test ()

    mytest = Obj.func # Declare local variable

    n = 2

    for i in range (n):

    mytest (i) # faster than Obj.func (i)