Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Math 480: Open Source Mathematical Software
2016-04-11
William Stein
Lectures 7: Speed (part 1 of 3)
Or... why slower is often faster.
Plan:
Monday: benchmarking, using numpy and other libraries to make code factors
Wednesday: Cython
Friday: Other performance optimization tools (e.g., numba, pypy, Psycho?, etc.?)
Announcements
Start the screencast!!!! Unless you know for a fact otherwise, I forgot.
TA office hours: Thursday
"Virtual" office hours: email math480-2016-sage@googlegroups.com, wstein@uw.edu, or john.jeng5@gmail.com
Type in a chat in any project -- the TA and I should see a notification and help you.
Homework 2 will be collected at (or after) about 6pm tonight. Make sure your work is there, since recent days were a disaster (not any more!)
Peer grading will happen.
Note: filenames ending in ~ -- when updating an assignment the old files have a ~. We updated today's assignment right after making it.
This morning I woke up and read on Hacker News
"Python is slow. Painfully slow. Slow as in "it stores the entire AST at runtime and lets you modify it while the program runs" slow. But it's also great at numerical computation with numpy. And it's enormously flexible (which is what makes it slow). And it's clean. It writes really well. For a quick mathematical processing program or website whose code needs to be legible, it's great." -- robert.stolarz@yandex.com
Response by somebody else:
"Python doesn't store the entire AST at runtime. You can hook into the import process and modify code before it's compiled to bytecode. You can modify the bytecode at runtime but not any that's on the stack. Python is also not all that slow, CPython is slow-ish but it's has effectively no optimizations at all. PyPy which has decent optimizations is actually fairly fast and getting faster with every release." -- DasIch
Timing your code
Here's some code. It is intentionally naive in various ways...
Explain this code line by line:
define function of one input
Docstring
sin(m) is the sage symbolic sin function
(sin(m) for m in range(n))
is like a list comprehension but it is "lazy" so it doesn't waste memory -- you can iterative over it though.sum(...)
iterates over its input summing it upfloat(...)
converts its input to a float
Throw out a mathematical question: Is there a simple formula for similar to the formula ?
Is it slow?
Or is the network (or SMC) just slow at sending the answer back?
If it is slow, why?
There are many ways to time execution of code:
Very naive: use a stopwatch. Start the code running and start a stopwatch on your phone or whatever. Look at the wall clock.
Python: use the
time
module, and printsPython: use the
timeit
module, and better printsIn worksheets:
%time
and%timeit
In Sage:
cputime
andwalltime
functions.Using
%prun
in a terminal (so create a terminal and type "sage"). This is an ipython feature.
And there are also many, many ways to speed up code...
timeit module: see https://docs.python.org/2/library/timeit.html
Use cputime and walltime
Discuss the difference between cpu and wall time.
Using %prun in a terminal
+New --> Terminal.
type
sage
to start sage.Define the function.
Use
%prun sin_sum(10000)
Exercise: Try to make sin_sum faster and time the result in several ways. Ideas for how:
replace
m
byfloat(m)
replace
sin
bymath.sin