Python Advanced Programming (12th: Optimizing learning) 1

Source: Internet
Author: User

#-*-Coding:utf-8-*-
# python:2.x
__author__ = ' Administrator '
#由于5, 6,7,8,9,10,11 is mainly in the package, testing and other studies so this side does not study

#优化: general principles and profiling techniques
Print (U ' premature optimization is the source of all evil in programming-------Donald Knuth ')

#1: Priority Three Principles
"""
Regardless of the outcome, optimization is a cost, and when the code is working, it may be better to try to run it faster than at all costs.
When optimizing code, 3 principles are required:
1: First can make it work properly
2: From the user's point of view
3: Maintain easy-to-read code style
"""

#首先能够让它正常工作
"""
It is not possible to be the most common error when writing code and optimizing code on one side, because bottlenecks tend to occur unexpectedly
Applications are made up of a lot of complex interactions, and it's impossible to get a complete view before actually using it.
Of course, this is not a reason not to try to write faster functions or methods as much as possible. You should seriously make it as low-complexity as possible and avoid unnecessary duplication, so you can get it working properly first!
For line-level code, the Python philosophy is to accomplish a goal as much as possible and there is only one way, so as long as you stick to the Python style syntax introduced in chapters 2nd and 3rd, the code that is often written will be better and faster
Don't do the following before you dissect it.
1: Start writing a global dictionary for the function cache data
2: Consider using a mixed language such as C or Pyrex to extend one part of the code
3: Look for external libraries to complete some basic calculations
"""

#扩展模块: Numeric can simplify development and generate simple, faster code, and should not rewrite a function when there is a good library to use
#Soya 3d is based on the OpenGL game engine, developed using C and Pyrex, to perform a fast rectangular operation when displaying real-time 3D graphics
#Soya 3d official: (http://home.gna.org/oomadness/en/soya3d/index.html)
The #OpenAL (Open audio Library) is a cross-platform sound API for the free software community. It is designed for multi-channel three-dimensional position effect performance, some software needs this feature support,
#GLEW (is a cross-platform C + + extension library, based on the OpenGL graphics interface. ), Description: http://blog.csdn.net/pythonfx/article/details/9771047
#SDL (is a set of open source cross-platform multimedia Development Library, written in C language. SDL provides several functions for controlling images, sounds, and outputs so that developers can develop applications across multiple platforms (Linux, Windows, Mac OS x, etc.) with the same or similar code. SDL is currently used to develop multimedia applications such as games, simulators, media players, and more. ): Http://zh.wikipedia.org/wiki/SDL
#Cal3D (Skeletal animation system Cal3d) Official: http://home.gna.org/cal3d/
#libFreeType2 (a free software library renders fonts) official: http://www.freetype.org
#freefont (Python captcha generation tool module) Official: http://fontfabric.com, Example: http://www.oschina.net/code/snippet_129830_17856
#ode (an open-source, high-performance library simulates rigid body dynamics. Its full-featured, stable, mature, and standalone platform makes it easy to use C + + APIs. It has advanced joint types and integrated and friction collision detection. Carols are used to simulate the environment of vehicles in virtual object reality and virtual organisms. Currently used in many computer games, 3 D authoring tools and simulation tools. ) Official: http://www.ode.org
#更多请看: http://home.gna.org/oomadness/en/soya3d/index.html
Print (' optimization should be done on a program that's already working, it works first, then makes it better, it's better to make it faster---Kent Beck ')


#从用户观点进行
"""
While it is a good thing to be able to start a program better and faster, you should carefully prioritize the optimization and ask the following questions:
1: Whether the customer asks for speed
2: Who found the program to be slow?
3: How much does it cost to speed up? Which parts need to be speeded up?
Remember: Optimization is a cost, and the developer's point of view is that it may not make sense to the customer, unless written as a framework or library, and the customer is a developer!
Note: Optimization is not a game, it should only be done when necessary
"""

#保持代码易读 (thus easy to maintain)
"""
When the optimization to a certain goal is, if there is still a part of the understanding, those should stop working and seek other solutions may be a good idea!
Optimization should not make the code difficult to understand, if this happens, you should seek alternative methods, such as expansion or redesign, but there is always a good compromise in code legibility and speed
"""

Python Advanced Programming (12th: Optimizing learning) 1

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.