Skip to Content
Python高级编程(第2版)
book

Python高级编程(第2版)

by Posts & Telecom Press, Michał Jaworski, Tarek Ziadé
February 2024
Intermediate to advanced
423 pages
6h 9m
Chinese
Packt Publishing
Content preview from Python高级编程(第2版)

第12章 优化——一些强大的技术

优化程序不是一个神奇的过程。遵循一个简单的算法就可以完成,这个算法由Stefan Schwarzer在Europython 2006上综合给出,他的原始伪代码示例如下:

   def optimize():
       """推荐的优化过程"""
       assert got_architecture_right(), "fix architecture"
       assert made_code_work(bugs=None), "fix bugs"
       while code_is_too_slow():
           wbn = find_worst_bottleneck(just_guess=False,
                                       profile=True)
           is_faster = try_to_optimize(wbn,
                                       run_unit_tests=True,
                                       new_bugs=None)
           if not is_faster:
               undo_last_code_change()

   # By Stefan Schwarzer, Europython 2006

这个例子可能不是最整洁,最清晰的,但是它几乎抓住了一个有组织的优化过程的所有的重要方面。从中我们可以学到的主要内容如下。

  • 优化是一个迭代过程,在这个过程中,并不是每次迭代都会有更好的结果。
  • 主要先决条件是通过测试验证并且正常工作的代码。
  • 你应该始终专注于优化当前的应用程序的瓶颈。

让你的代码工作地更快不是一个容易的任务。如果是抽象的数学问题,解决方案当然在于选择正确的算法和适当的数据结构。但在这种情况下,很难提供一些通用的提示和技巧,可以在任何代码中用于解决算法问题。当然有一些通用的方法来设计一个新的算法,甚至是可以应用于各种各样的问题的元启发式算法,但它们是语言无关的,因此不在本书的范围。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Hadoop实际解决方案手册

Hadoop实际解决方案手册

Posts & Telecom Press, JONATHAN OWENS, Lentz Jon, Femiano Brian
TensorFlow机器学习

TensorFlow机器学习

Posts & Telecom Press, Quan Hua, Shams Ul Azeem, Saif Ahmed
Python和NLTK实现自然语言处理

Python和NLTK实现自然语言处理

Posts & Telecom Press, Nitin Hardeniya
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty

Publisher Resources

ISBN: 9781835887745