Python high Performance programming method one

Source: Internet
Author: User
Tags python list

Python high Performance programming method one reads Zen of Python and enters import this in the Python parser. A sharp Python novice might notice the word "parsing", thinking that Python is just another script ...

Read Zen of Python and enter import this in the Python parser. A sharp Python novice might notice the word "parsing", thinking that Python is just another scripting language. "It must be slow!"

There is no doubt that Python programs are fast and efficient without a compiled language. Even Python advocates will tell you that Python is not suitable for these areas. However, YouTube has used python to serve 40 million video requests per hour. All you have to do is write efficient code and use the external implementation (c + +) code when needed. Here are some tips to help you become a better Python developer:

1. Use the built-in function: You can write efficient code in Python, but it's hard to beat the built-in function. Verified.      They are very fast. 2. Use the Join () connection string. You can use "+" to concatenate strings. But because string is immutable in Python, each "+" operation creates a new string and copies the old content. A common usage is to use the Python array module to modify the characters individually, and when finished, use the join () function to create the final string.

>>> #This is good to glue a large number of strings

>>> for chunk in input ():

>>> My_string.join (Chunk)

3. Use Python multiple assignments to swap variables

This is elegant and fast in Python:

>>> x, y = y, X

This is slow:

>>> temp = x

>>> x = y

>>> y = Temp

4. Use local variables as much as possible

Python retrieves local variables faster than retrieving global variables. This means avoiding the "global" keyword.

5. Use "in" as much as possible

Use the "in" keyword. Simple and fast.

>>> for key in sequence:

>>> print "Found"

6. Using Lazy load acceleration

Move the "import" declaration into the function and import it only when needed. In other words, if some modules do not need to be used immediately, import them later. For example, you do not have to import a large number of modules in a single drive to speed up the program. This technology does not improve overall performance. But it can help you more evenly distribute the load time of the module.

7. Use "while 1" for infinite loops

Sometimes you need an infinite loop in the program. (such as an instance of a listening socket) although "while true" can do the same thing, "while 1" is a single-step operation. This will improve your Python performance.

>>> while 1:

>>> #do stuff, faster with while 1

>>> while True:

>>> # do stuff, slower with Wile True

8. Using the list comprehension

Starting with Python 2.0, you can use the list comprehension instead of a large number of "for" and "while" blocks. Using the list comprehension is usually faster, and the Python parser is optimized to see it as a predictable pattern in the loop. The additional benefit is that list comprehension is more readable (functional programming) and, in most cases, it saves an extra count variable. For example, let's calculate an even number of numbers between 1 and 10:

>>> # The good to iterate a range

>>> evens = [i-I in range] if i%2 = = 0]

>>> [0, 2, 4, 6, 8]

>>> # The following is not so Pythonic

>>> i = 0

>>> evens = []

>>> While I < 10:

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

>>> i + = 1

>>> [0, 2, 4, 6, 8]

9. Use Xrange () to process long sequences:

This can save you a lot of system memory because Xrange () produces only one integer element per call in the sequence. Instead of the range (), it will give you a complete list of elements that can be used for loops with unnecessary overhead.

10. Use Python Generator:

This can also save memory and improve performance. For example a video stream, you can send a byte block instead of the entire stream. For example,

>>> Chunk = (+ * I for I in xrange (1000))

>>> Chunk

<generator Object at 0x7f65d90dcaa0>

>>> Chunk.next ()

0

>>> Chunk.next ()

1000

>>> Chunk.next ()

2000

11. Learn about the Itertools module:

This module is very effective for iterations and combinations. Let's generate a list of all permutations of [all-in-one], with just three lines of Python code:

>>> Import Itertools

>>> iter = itertools.permutations ([+])

>>> List (ITER)

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

12. Learn the Bisect module to keep the list sorted:

This is a free binary search tool that implements and quickly inserts an ordered sequence. In other words, you can use:

>>> Import bisect

>>> bisect.insort (list, Element)

You have inserted an element into the list, and you do not need to call sort () again to keep the container sorted, because this can be very expensive in a long sequence.

13. Understand the Python list, which is actually an array:

The list implementations in Python are not implemented in a common single-linked list of computer science that people usually talk about. The list in Python is an array. That is, you can retrieve an element of the list in constant time O (1) without having to search from scratch. What's the point? Python developers need to think twice when using the list object Insert (). For example:>>> List.insert (0,item)

Inserting an element in front of the list is inefficient because all subsequent subscripts in the list have to be changed. However, you can use List.append () to effectively add elements at the end of the list. Pick First deque If you want to quickly insert or when in two. It is fast because the deque in Python is implemented with a doubly linked list. Say no more.

14. Use Dict and set test members: checks whether an element is present in dicitonary or set, which is very fast in python. This is because Dict and set are implemented using a hash table. The search efficiency can reach O (1). Therefore, if you need to check members frequently, use set or dict as your container.

>>> mylist = [' A ', ' B ', ' C '] #Slower, check membership with list:

>>> ' C ' in MyList

>>> True

>>> MySet = set ([' A ', ' B ', ' C ']) # Faster, check membership with set:

>>> ' C ' in MySet:

>>> True

15. Use Schwartzian Transform's sort ():

The native List.sort () function is very fast. Python sorts the list in natural order. Sometimes, you need to sort in unnatural order. For example, you want to sort the IP address according to the server location. Python supports custom comparisons, you can use List.sort (CMP ()), which is slower than list.sort (), because it increases the overhead of function calls. If there is a problem with performance, you can apply Guttman-rosler Transform, based on Schwartzian Transform. It is only interested in the actual algorithm to be used, and it briefly works, you can transform the list and call Python built-in List.sort ()-> faster, without using List.sort (CMP ())-Slow.

The Python adorner caches the results:

The "@" symbol is the decorative syntax for python. It is not only used for tracing, locking or logging. You can decorate a Python function and remember to invoke the result for later use. This technique is called memoization. Here is an example:

>>> from Functools Import wraps

>>> def memo (f):

>>> cache = {}

>>> @wraps (f)

>>> def Wrap (*arg):

>>> if arg not in cache:cache[' arg '] = f (*arg)

>>> return cache[' arg ']

>>> return Wrap

We can also use adorners with the Fibonacci function:

>>> @memo

>>> def fib (i):

>>> If I < 2:return 1

>>> return fib (i-1) + fib (i-2)

The key idea here is to enhance the function ( decoration) function, remembering each Fibonacci value that has been computed, and if they are in the cache, no more calculations are needed.

17. Understanding Python's Gil (Global Interpreter Lock):

Gil is necessary because CPython's memory management is non-thread safe. You can't simply create multiple threads and expect Python to run faster on multi-core machines. This is because the Gil will prevent multiple native threads from executing python bytecode at the same time. In other words, the Gil will serialize all your threads. However, you can use threads to manage multiple derived process accelerators, which run independently of your Python code.

18. Familiar with Python source code as familiar with the document:

Some of the Python modules are implemented for performance using C. When performance is critical and official documentation is insufficient, you can freely explore the source code. You can find the underlying data structures and algorithms. Python's source library is a great place to be: Http://svn.python.org/view/python/trunk/Modules

Conclusion:

These are no substitute for brain thinking. Opening the Hood it's a developer's job to get a good idea, so they don't quickly piece together a garbage design. The Python recommendations in this article can help you get good performance. If the speed is not fast enough, Python will need an external force: analyze and run outside code. We'll cover this in part two of this article.

Python high Performance programming method one

Related Article

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.