Issue
%%timeit
in IPython or Jupyter notebook has 2 features I like.
Run for a limited time, adjust the number of iterations automatically for a given number of batches.
Output is mean + standard deviation. That's nicer than the minimum run time among many runs.
Here is example output from an IPython terminal. The first one is from a "slow version" of a function, the second is from a faster version.
In [21]: %timeit hh = gg.get_map(form="232", year=2019, source="form")
1.82 s ± 111 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [18]: %timeit jj = fm.get_map(form="232", year=2019, source="form")
324 µs ± 22.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Note that when the function was slow, it gave back 7 x 1 execution times, but when it was fast it dialed up to 7 x 1000.
I've been studying the IPython code to understand how they do that, thinking I could adapt for a block of python code. This is difficult work! It is taking me into unfamiliar territory interacting with AST.
It seems to me like somebody must have done this before. Is there no Python package for this?
Solution
The short answer is that you can't do this in a py file/block of pure Python code.
Once the file is parsed, you can't get the AST back from Python. This is one of the reason "macros", are an oft requested feature in Python, as they would let you do things like this.
There are likely hacky way of doing it, but roughly speaking you will need to find the file in which the code is defined, and get the content as a string to manipulate it; what IPython can do as it does its own parsing.
.... or you make an API that takes a multiline string instead of a block of Python code, which is ugly but could work.
More generally if you think there are block of code we could make more reusable in IPython, feel free to open an issue/pull requests.
Answered By - Matt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.