Python Note-python programming Optimization: Common principles and technical introduction

Source: Internet
Author: User

I translated from "Exper Python Programming"

' Premature optimization is the root of all evil in programming '-donald Knuth

Three Principles of optimization

    • Let it run, first.

One of the most common mistakes is that we begin to optimize the code at the beginning of the code. The sad thing is that this is usually done useless work, a lot of software only you run up to find its real bottleneck where.

    • Look at the problem from the user's point of view

There was a team that ran faster for their projects and eventually reached their satisfaction, eventually delivering it to the customer, and found that the customer didn't think it would be good for him to run fast ...

    • Always keep code readable (maintainable)

If you have completed 90% of the optimization tasks, while the remaining 10% of the optimization tasks may make your code completely unreadable, then you might want to stop your optimization work.

Optimized strategy

    • To find someone else's problem.

In general, our software testing is not possible to achieve 100% simulation of the real environment. When a customer complains to us that the software is not running as fast as before, we might as well look for the speed problems that other applications have caused when interacting with other applications.

    • Brush Hardware

This people are understood, running slowly? First see if you are not configured enough Ah, enough to buy buy buy!

    • Write a Speed test document

When we start to optimize our work, we might as well write a comment under our objective function "the function cannot run longer than 1 seconds"

Looking for bottlenecks

    • View CPU Usage

From the perspective of the CPU, it is divided into macro-search and microscopic search. The most common here is cprofile, a C-language library that provides a record of the time it takes to monitor the use of program resources. The specific use method can point here.

    • View Memory Utilization

Since we're talking about python, we're going to have to mention Python's memory allocation mechanism, and unlike the C language, we can use a function to know the memory allocation of the current variable, and in Python we never know how much memory space a variable is currently allocating. Typically, memory space is consumed for the following reasons: 1. An uncontrolled growth of a variable; 2. There are too many instances in the global, and they do not monitor their use of memory; 3. The thread that does not shut down properly; 4. Objects with __del__ properties in a loop body

The specific memory detection tools are: Guppy, heapy

    • View Network usage

This part of the story we use the Universal Network monitoring tools just fine.

reduce the complexity of your code  

Two dimensions:

    • Measure Branch Complexity: How many branches of a ifelse statement in a program
    • Measurement of time space complexity

The last two major themes are: multithreading and caching. These two methods are also very important to the optimizer method, first written here, read on to continue more.

Python Note-python programming Optimization: Common principles and technical introduction

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.