sagemathinc / cocalc

CoCalc: Collaborative Calculation in the Cloud
https://CoCalc.com
Other
1.17k stars 216 forks source link

support %lprun in sage worksheets #2129

Open haraldschilly opened 7 years ago

haraldschilly commented 7 years ago

Supposedly a line profiler, there is also %prun, though.

Ref: https://twitter.com/vit_tucek/status/877084507119247360

williamstein commented 7 years ago

Can somebody also come up with an example? I wonder if this "just works" already in our Jupyter notebooks, and also when using a kernel through a Sage worksheet?

haraldschilly commented 7 years ago

Here is a contrived example, running in Anaconda Python 3 on CoCalc. It's basically the line_profiler:

def f(x):
    return 2*x +1
def g(a, n):
    b = f(a)
    for i in range(n):
        b = f(a) + f(b)
    return b
---
%load_ext line_profiler
---
%lprun -f g g(5, 100)
---
%lprun -f f g(5, 100)  # notice, 200 hits for f, when calling g(5,100)

jupyter-lprun

vit-tucek commented 7 years ago

It works in console version of sage. It is also mentioned in the Sage documentation on profiling.

http://doc.sagemath.org/html/en/thematic_tutorials/profiling.html#python-level-line-by-line-profiling-lprun