Cython - Helion
ISBN: 978-14-919-0175-5
stron: 254, Format: ebook
Data wydania: 2015-01-21
Księgarnia: Helion
Cena książki: 92,65 zł (poprzednio: 107,73 zł)
Oszczędzasz: 14% (-15,08 zł)
Build software that combines Python’s expressivity with the performance and control of C (and C++). It’s possible with Cython, the compiler and hybrid programming language used by foundational packages such as NumPy, and prominent in projects including Pandas, h5py, and scikits-learn. In this practical guide, you’ll learn how to use Cython to improve Python’s performance—up to 3000x— and to wrap C and C++ libraries in Python with ease.
Author Kurt Smith takes you through Cython’s capabilities, with sample code and in-depth practice exercises. If you’re just starting with Cython, or want to go deeper, you’ll learn how this language is an essential part of any performance-oriented Python programmer’s arsenal.
- Use Cython’s static typing to speed up Python code
- Gain hands-on experience using Cython features to boost your numeric-heavy Python
- Create new types with Cython—and see how fast object-oriented programming in Python can be
- Effectively organize Cython code into separate modules and packages without sacrificing performance
- Use Cython to give Pythonic interfaces to C and C++ libraries
- Optimize code with Cython’s runtime and compile-time profiling tools
- Use Cython’s prange function to parallelize loops transparently with OpenMP
Osoby które kupowały "Cython", wybierały także:
- Windows Media Center. Domowe centrum rozrywki 66,67 zł, (8,00 zł -88%)
- Ruby on Rails. Ćwiczenia 18,75 zł, (3,00 zł -84%)
- Przywództwo w świecie VUCA. Jak być skutecznym liderem w niepewnym środowisku 58,64 zł, (12,90 zł -78%)
- Scrum. O zwinnym zarządzaniu projektami. Wydanie II rozszerzone 58,64 zł, (12,90 zł -78%)
- Od hierarchii do turkusu, czyli jak zarządzać w XXI wieku 58,64 zł, (12,90 zł -78%)
Spis treści
Cython eBook -- spis treści
- Cython
- Dedication
- Preface
- Who Should Read This Book?
- Prerequisites
- Who Should Not Read This Book?
- Outline
- Conventions Used in This Book
- Using Code Examples
- Safari Books Online
- How to Contact Us
- Acknowledgments
- Who Should Read This Book?
- 1. Cython Essentials
- Comparing Python, C, and Cython
- Function Call Overhead
- Looping
- Math Operations
- Stack Versus Heap Allocation
- Tempering Our Enthusiasm
- Wrapping C Code with Cython
- Summary
- Comparing Python, C, and Cython
- 2. Compiling and Running Cython Code
- The Cython Compilation Pipeline
- Installing and Testing Our Setup
- C and C++ compilers
- Installing Cython
- Installing and Testing Our Setup
- The Standard Way: Using distutils with cythonize
- Our distutils Script
- Compiling with distutils on Mac OS X and Linux
- Compiling with distutils on Windows
- Using Our Extension Module
- Interactive Cython with IPythons %%cython Magic
- Compiling On-the-Fly with pyximport
- Controlling pyximport and Managing Dependencies
- pyximport Example with External Dependencies
- Rolling Our Own and Compiling by Hand
- Using Cython with Other Build Systems
- CMake and Cython
- SCons and Cython
- Make and Cython
- Compiler Directives
- Summary
- The Cython Compilation Pipeline
- 3. Cython in Depth
- Interpreted Versus Compiled Execution
- Dynamic Versus Static Typing
- Static Type Declaration with cdef
- Automatic Type Inference in Cython
- C Pointers in Cython
- Mixing Statically and Dynamically Typed Variables
- The bint type
- Integral type conversions and overflow
- Floating-point type conversions
- Complex types
- bytes type
- str and unicode types
- Statically Declaring Variables with a Python Type
- Static Typing for Speed
- Reference Counting and Static String Types
- Cythons Three Kinds of Functions
- Python Functions in Cython with the def Keyword
- C Functions in Cython with the cdef Keyword
- Combining def and cdef Functions with cpdef
- Functions and Exception Handling
- Functions and the embedsignature Compiler Directive
- Type Coercion and Casting
- Declaring and Using structs, unions, and enums
- Type Aliasing with ctypedef
- Cython for Loops and while Loops
- Guidelines for Efficient Loops
- Loop Example
- The Cython Preprocessor
- Bridging the Python 2 and Python 3 Divide
- str, unicode, bytes, and All That
- Summary
- 4. Cython in Practice: N-Body Simulation
- Overview of the N-Body Python Code
- Converting to Cython
- Python Data Structures and Organization
- Converting Data Structures to structs
- Running the Cythonized Version
- Summary
- 5. Cython and Extension Types
- Comparing Python Classes and Extension Types
- Extension Types in Cython
- Type Attributes and Access Control
- C-Level Initialization and Finalization
- cdef and cpdef Methods
- Inheritance and Subclassing
- Casting and Subclasses
- Extension Type Objects and None
- Extension Type Properties in Cython
- Special Methods Are Even More Special
- Arithmetic Methods
- Rich Comparisons
- Iterator Support
- Summary
- 6. Organizing Cython Code
- Cython Implementation (.pyx) and Declaration (.pxd) Files
- The cimport Statement
- Predefined Definition Files
- Using cimport with a module in a package
- Using cimport with an object from a dotted module name
- Multiple named cimports
- Using cimport with an alias
- Using cimport with C++ STL template classes
- Predefined Definition Files
- Include Files and the include Statement
- Organizing and Compiling Cython Modules Inside Python Packages
- Summary
- 7. Wrapping C Libraries with Cython
- Declaring External C Code in Cython
- Cython Does Not Automate Wrapping
- Declaring External C Functions and typedefs
- Declaring and Wrapping C structs, unions, and enums
- Wrapping C Functions
- Wrapping C structs with Extension Types
- Constants, Other Modifiers, and Controlling What Cython Generates
- Error Checking and Raising Exceptions
- Callbacks
- Callbacks and Exception Propagation
- Summary
- Declaring External C Code in Cython
- 8. Wrapping C++ Libraries with Cython
- Simple Example: MT_RNG Class
- The Wrapper Extension Type
- Compiling with C++
- Using Our Wrapper from Python
- Overloaded Methods and Functions
- Operator Overloading
- C++ Exceptions
- Stack and Heap Allocation of C++ Instances
- Working with C++ Class Hierarchies
- C++ Templates
- Templated Functions and Cythons Fused Types
- Templated Classes
- Iterators and Nested Classes
- Included STL Container Class Declarations
- Memory Management and Smart Pointers
- Summary
- Simple Example: MT_RNG Class
- 9. Cython Profiling Tools
- Cython Runtime Profiling
- Performance Profiling and Annotations
- Summary
- 10. Cython, NumPy, and Typed Memoryviews
- The Power of the New Buffer Protocol
- The memoryview Type
- Typed Memoryviews
- Typed Memoryview Example
- C-Level Access to Typed Memoryview Data
- Trading Safety for Performance
- Declaring Typed Memoryviews
- Using Typed Memoryviews
- Beyond Buffers
- Wrapping C and C++ Arrays
- Correct (and Automatic) Memory Management with Cython and C Arrays
- Summary
- The Power of the New Buffer Protocol
- 11. Cython in Practice: Spectral Norm
- Overview of the Spectral Norm Python Code
- Performance Profiling
- Cythonizing Our Code
- Adding Static Type Information
- Using Typed Memoryviews
- Comparing to the C Implementation
- Summary
- 12. Parallel Programming with Cython
- Thread-Based Parallelism and the Global Interpreter Lock
- The nogil Function Attribute
- The with nogil Context Manager
- Using prange to Parallelize Loops
- Using prange
- prange Options
- Using prange for Reductions
- Parallel Programming Pointers and Pitfalls
- Summary
- Thread-Based Parallelism and the Global Interpreter Lock
- 13. Cython in Context
- Cython Versus Project X
- Other Ahead-of-Time Compilers for Python
- Python Wrapper Projects
- Just-in-Time Compilers for Python
- Summary
- Cython Versus Project X
- Index
- Colophon
- Copyright