This is a creation in Article, where the information may have evolved or changed.
General programming language Performance Comparison This site is more persuasive http://benchmarksgame.alioth.debian.org/u64q/ Ruby.html, the current situation is that the speed of Python is at the bottom of the comparison list programming language, but this is not to say that Python is not desirable, pytthon slow to work, through some simple optimizations and coding specifications Python's speed is still good, all day long in the mouth Python performance is small White, do you want to do a system that requires millions of concurrent? Need an error in real-time trading within 10 milliseconds? Do I need to scan the Tens server port? Need a DDoS-level stress test? Do you need a 100 million-time weak password explosion in a few hours? If you need, 100 million-level demolition and production environment of 10000QPS individuals have been implemented in the way of python+c extension, I can help you.
In fact, most of the scenes are Python can work very well, some of the performance requirements of the scene through the Python+c expansion of the form can be done, and save time and effort.
There was always a member of the group complaining all the day, Python performance how bad, how hard it is to use it, instead of looking for performance bottlenecks where to do optimization. to remember, their own food, blame programming language is no use, it is like squat pit, constipation pull out, said toilet this place earth attraction is too small , heard later that friend to learn Scala, and others said he went to learn Golang, I wish him to find the perfect language in his heart soon.
- Coding specifications for Python
- connect the generator and list derivation
- strings If the number is within 3 or 3, directly with +, more than 3, with format or join,join the best, but format and join difference is not very large
- Do not write recursive code, tail recursion also do not, change to the form of a loop
- tuple can solve the problem do not use list, the same set can solve the problem do not use dict, but dict and list, compared to the dict speed, is the memory occupied large. But do not deliberately go to the list to a tuple. The
- function can use local variables, do not use global variables
- to exchange variables in pythonic way, A,b=b,a
- map can sometimes replace loops, this time should not hesitate to use the map
- Minimize unnecessary non-circular steps within the loop
- use while 1 to use while True
- to compare multiple variable values with a<b<c,a<b<c more efficient than a<b and b<c
- calculates the y power of X with X**y, do not use POW, difficult to read slower
- CPU-intensive computing do not use Python, even if you want to use, but also choose NumPy and other libraries, it is best to change the CPU-intensive part of the C code, and then cTYPES call, The CPU-intensive section is also not recommended Cython, in short, C extension >numpy=cython>python
Some specifications and language-independent, that is, c if your program logic is a problem, or slow.