High Performance Python(2).pdf

(8875 KB) Pobierz
Python
PRACTICAL PERFORMANT
PROGRAMMING FOR HUMANS
High Performance
Micha Gorelick & Ian Ozsvald
www.it-ebooks.info
High Performance Python
Your Python code may run correctly, but you need it to run faster. By
exploring the fundamental theory behind design choices, this practical
guide helps you gain a deeper understanding of Python’s implementation.
You’ll learn how to locate performance bottlenecks and significantly speed
up your code in high-data-volume programs.
How can you take advantage of multi-core architectures or clusters?
Or build a system that can scale up and down without losing reliability?
Experienced Python programmers will learn concrete solutions to these
and other issues, along with war stories from companies that use high
performance Python for social media analytics, productionized machine
learning, and other situations.
Despite its popularity
in academia and
industry, Python is often
dismissed as too slow for
real applications. This
book sweeps away that
misconception with a
thorough introduction
to strategies for fast and
scalable computation
with Python.
University of Washington
Get a better grasp of numpy, Cython, and profilers
Learn how Python abstracts the underlying computer
architecture
Use profiling to find bottlenecks in CPU time and memory usage
Write efficient programs by choosing appropriate data
structures
Speed up matrix and vector computations
Use tools to compile Python down to machine code
Manage multiple I/O and computational operations concurrently
Convert multiprocessing code to run on a local or remote cluster
Solve large problems while using less RAM
—Jake VanderPlas
Micha Gorelick,
winner of the Nobel Prize in 2046 for his contributions to time
travel, went back to the 2000s to study astrophysics, work on data at bitly, and
co-found Fast Forward Labs as resident Mad Scientist, working on issues from
machine learning to performant stream algorithms.
Ian Ozsvald
is a data scientist and teacher at ModelInsight.io, with over ten years
of Python experience. He’s taught high performance Python at the PyCon and
PyData conferences and has been consulting on data science and high perfor-
mance computing for years in the UK.
PY THON / PERFORMANCE
Twitter: @oreillymedia
facebook.com/oreilly
US $39.99
CAN $41.99
ISBN: 978-1-449-36159-4
www.it-ebooks.info
High Performance Python
Micha Gorelick and Ian Ozsvald
www.it-ebooks.info
High Performance Python
by Micha Gorelick and Ian Ozsvald
Copyright © 2014 Micha Gorelick and Ian Ozsvald. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://safaribooksonline.com/). For more information, contact our corporate/
institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Editors:
Meghan Blanchette and Rachel Roumeliotis
Production Editor:
Matthew Hacker
Copyeditor:
Rachel Head
Proofreader:
Rachel Monaghan
September 2014:
First Edition
Indexer:
Wendy Catalano
Cover Designer:
Karen Montgomery
Interior Designer:
David Futato
Illustrator:
Rebecca Demarest
Revision History for the First Edition:
2014-08-21:
First release
See
http://oreilly.com/catalog/errata.csp?isbn=9781449361594
for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc.
High Performance Python,
the image of a fer-de-lance, and related trade dress are trademarks
of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-36159-4
[LSI]
www.it-ebooks.info
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Understanding Performant Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Fundamental Computer System
Computing Units
Memory Units
Communications Layers
Putting the Fundamental Elements Together
Idealized Computing Versus the Python Virtual Machine
So Why Use Python?
1
2
5
7
9
10
13
18
19
23
26
29
31
36
37
42
48
50
52
54
56
57
59
60
2. Profiling to Find Bottlenecks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Profiling Efficiently
Introducing the Julia Set
Calculating the Full Julia Set
Simple Approaches to Timing—print and a Decorator
Simple Timing Using the Unix time Command
Using the cProfile Module
Using runsnakerun to Visualize cProfile Output
Using line_profiler for Line-by-Line Measurements
Using memory_profiler to Diagnose Memory Usage
Inspecting Objects on the Heap with heapy
Using dowser for Live Graphing of Instantiated Variables
Using the dis Module to Examine CPython Bytecode
Different Approaches, Different Complexity
Unit Testing During Optimization to Maintain Correctness
No-op @profile Decorator
Strategies to Profile Your Code Successfully
Wrap-Up
iii
www.it-ebooks.info
Zgłoś jeśli naruszono regulamin