LECTURE 20 Optimizing Python. THE NEED FOR SPEED By now, hopefully I’ve shown that Python is an extremely versatile language that supports quick and easy.

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

C Language.
Recursion CS 367 – Introduction to Data Structures.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
Tutorial C#. PLAN I. Introduction II. Example of C# program :Hello World III. How to use C# language GUI elements Primitives Types Expressions and operators.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Scientific Computing Beyond Matlab Nov 19, 2012 Jason Su.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Java Introduction to JNI Prepared by Humaira Siddiqui.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Writing Perl Extensions: There’s more than one way to do it.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Introduction to Information Security Python. Python motivation Python is to a Hacker what Matlab is to an engineer Lots of built-in modules Lots of 3.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
RUBY by Ryan Chase.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Object Oriented Software Development 4. C# data types, objects and references.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Java Programming Language Lecture27- An Introduction.
C++ Lesson 1.
C++ First Steps.
The need for Programming Languages
Lecture 20 Optimizing Python.
PROGRAMMING LANGUAGES
Lecture 2 Python Basics.
A bit of C programming Lecture 3 Uli Raich.
Introduction to Python
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Introduction to C Topics Compilation Using the gcc Compiler
12 Data abstraction Packages and encapsulation
void Pointers Lesson xx
Methods and Parameters
Tutorial C#.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Lecture 22 Inheritance Richard Gesick.
Introduction to Python
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Pointers, Dynamic Data, and Reference Types
Chapter 11 Introduction to Programming in C
Type & Typeclass Syntax in function
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CISC/CMPE320 - Prof. McLeod
Java Programming Language
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Presentation transcript:

LECTURE 20 Optimizing Python

THE NEED FOR SPEED By now, hopefully I’ve shown that Python is an extremely versatile language that supports quick and easy development. However, a lot of the nice features that make it pleasant to develop with have a high cost behind the scenes. As a result, one of Python’s major drawbacks is its speed. Even for activities at which Python excels, like string manipulation, Python falls way behind in the category of “faster” languages. For a particular String Manipulation Benchmark, the following time results were achieved for a 4096KB string size: 0:07:17 (Perl), 0:31:09 (PHP), 0:40:55 (Ruby), 0:45:20 (Python), 0:28:51 (C++), 0:09:15 (C).

STRING MANIPULATION BENCHMARK Source:

CYTHON AND NUMBA So, what can be done? Aside from tiny improvements that can be made here-and- there within the code itself, we can also use compiling methods to speed up our code. Two options are: Cython, an optimizing static compiler as well as a compiled language which generates Python modules that can be used by regular Python code. Numba, a Numpy-aware optimizing just-in-time compiler. $ sudo apt-get install build-essential $ sudo apt-get install llvm $ pip install llvmpy $ pip install cython $ pip install numba

CYTHON In the simplest of terms: Cython is Python with C data types. Almost any piece of Python code is also valid Cython code, which the Cython compiler will convert into C code.

CYTHON Cython code must, unlike Python, be compiled. This happens in two stages: A.pyx file is compiled by Cython to a.c file, containing the code of a Python extension module. The.c file is compiled by a C compiler to a.so file (or.pyd on Windows) which can be imported directly into a Python session.

HELLO, WORLD! The basic steps to compiling a Cython extension are as follows: 1. In helloworld.pyx: print "Hello, World! 2. Create setup.py, your python “makefile”. 3. $ python setup.py build_ext –inplace  generates helloworld.so. 4. >>> import helloworld Hello, World! from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("helloworld.pyx") )

HELLO, WORLD! Alternatively, for typical modules that don’t require any extra C libraries, there is the pyximport method. >>> import pyximport >>> pyximport.install() >>> import helloworld Hello, World!

STATIC TYPING One of the main advantages of using Cython is to enforce static typing. By default, Python is obviously dynamically-typed but for performance-critical code, this may be undesirable. Using static typing allows the Cython compiler to generate simpler, faster C code. The use of static typing, however, is not “pythonic” and results in less-readable code so you are encouraged to only use static typing when the performance improvements justify it.

BASICS OF CYTHON The cdef statement is used to declare C variables, as well as C struct, union and enum types. cdef: int i, j, k float f, g[42], *h struct Node: int id float size union Data: char *str_data float *fl_data enum Color: red, blue, green cdef int i, j, k cdef float f, g[42], *h cdef struct Node: int id float size cdef union Data: char *str_data float *fl_data cdef enum Color: red, blue, green

BASICS OF CYTHON Cython supports all built-in C types as well as the special Cython types bint, used for C boolean values (int with 0/non-0 values for False/True), and Py_ssize_t, for (signed) sizes of Python containers. Also, the Python types list, dict, tuple, etc. may be used for static typing, as well as any user defined extension types.

STATIC TYPING Consider the following purely Python code. def f(x): return x**2-x def integrate_f(a, b, N): s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx

STATIC TYPING Consider the following purely Python code. def f(x): return x**2-x def integrate_f(a, b, N): s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx print(timeit.timeit("integrate_f(0.0, 5.0, )", setup="from cydemo import integrate_f", number=1)) Using Python’s timeit module, the call integrate_f(0, 5, ) took about seconds. By just compiling with Cython, the call took about seconds.

STATIC TYPING A Cythonic version of this code might look like this: def f(double x): return x**2-x def integrate_f(double a, double b, int N): cdef int i cdef double s, dx s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx # timeit code here Pure Python code took about seconds. By just compiling with Cython, the call took about seconds. By performing some static typing, the call took about.663 seconds. >>> import pyximport >>> pyximport.install() >>> import cydemo

CYTHON FUNCTIONS Python functions are defined using the def statement, as usual. They take Python objects as parameters and return Python objects. C functions are defined using the cdef statement. They take either Python objects or C values as parameters, and can return either Python objects or C values. def spam(python_i, python_s): cdef int i = python_i cdef char* s = python_s... Behind the scenes: def spam(int i, char *s):... cdef int eggs(unsigned long l, float f):...

CYTHON FUNCTIONS Within a Cython module, Python functions and C functions can call each other freely, but only Python functions can be called from outside the module by interpreted Python code. So, any functions that you want to “export” from your Cython module must be declared as Python functions using def. There is also a hybrid function, called cpdef. A cpdef function can be called from anywhere, but uses the faster C calling conventions when being called from other Cython code.

TYPING FUNCTIONS When using Cython, Python function calls are extra expensive because one might need to convert to and from Python objects to do the call. We can create some more speedup just by typing our functions. cdef double f(double x): return x**2-x def integrate_f(double a, double b, int N): cdef int i cdef double s, dx s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx # timeit code here >>> import pyximport >>> pyximport.install() >>> import cydemo

SOME RESULTS moduleN = N = cydemo cydemo cydemo cydemo cydemo: pure Python implementation. cydemo2: pure Python compiled with Cython. cydemo3: static typing. cydemo4: static typing and function typing.

CYTHON There is obviously a lot more to Cython but just knowing how to do some static typing and function typing is enough to gain some serious improvements in speed. If you’re interested, check here for the Cython documentation.here

NUMBA Numba provides a Just-In-Time compiler for Python code. Just-in-time compilation refers to the process of compiling during execution rather than before-hand. It uses the LLVM infrastructure to compile Python code into machine code. Central to the use of Numba is the numba.jit def f(x): return x**2-x def integrate_f(a, b, N): s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx # timeit function >>> import numbademo # N = numbademo.py

NUMBA JIT DECORATOR import numba from numba import float64, def f(x): return float64, int32)) def integrate_f(a, b, N): s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx # timeit function You can also specify the signature of the function. Otherwise Numba will generate separate compiled code for every possible type. >>> import numbademo # N =

SOME RESULTS moduleN = N = cydemo cydemo cydemo cydemo numbademo cydemo: pure Python implementation. cydemo2: pure Python compiled with Cython. cydemo3: static typing. cydemo4: static typing and function typing. numbademo: jit-compiled functions. Bottom-line: When it really matters, use Cython or Numba to improve your code’s speed. This is not quite a magic wand – these methods increase your dependencies, reduce your readability, and complicate your development. For simple examples like these, Cython and Numba are not too painful to add in, but they may be a headache for more complicated modules. See also Dropbox’s Pyston, a JIT compiler for Python.