Skip to content

Commit 4726291

Browse files
committed
Profiling
1 parent 27d79e6 commit 4726291

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,22 +2434,14 @@ duration = perf_counter() - start_time
24342434
0.34986
24352435
```
24362436

2437-
### Timing by Function
2438-
#### Generates a PNG image of a call graph with highlighted bottlenecks:
2439-
```python
2440-
# $ pip3 install pycallgraph
2441-
from pycallgraph import output, PyCallGraph
2442-
from datetime import datetime
2443-
time_str = datetime.now().strftime('%Y%m%d%H%M%S')
2444-
filename = f'profile-{time_str}.png'
2445-
drawer = output.GraphvizOutput(output_file=filename)
2446-
with PyCallGraph(drawer):
2447-
<code_to_be_profiled>
2448-
```
2449-
24502437
### Profiling by Line
2451-
```text
2452-
$ pip3 install line_profiler memory_profiler
2438+
```python
2439+
# $ pip3 install line_profiler memory_profiler
2440+
@profile
2441+
def main():
2442+
a = [*range(10000)]
2443+
b = {*range(10000)}
2444+
main()
24532445
```
24542446

24552447
```text
@@ -2472,6 +2464,19 @@ Line # Mem usage Increment Line Contents
24722464
4 36.160 MiB 0.426 MiB b = {*range(10000)}
24732465
```
24742466

2467+
### Call Graph
2468+
#### Generates a PNG image of a call graph with highlighted bottlenecks:
2469+
```python
2470+
# $ pip3 install pycallgraph
2471+
from pycallgraph import output, PyCallGraph
2472+
from datetime import datetime
2473+
time_str = datetime.now().strftime('%Y%m%d%H%M%S')
2474+
filename = f'profile-{time_str}.png'
2475+
drawer = output.GraphvizOutput(output_file=filename)
2476+
with PyCallGraph(drawer):
2477+
<code_to_be_profiled>
2478+
```
2479+
24752480

24762481
NumPy
24772482
-----

index.html

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,18 +2089,12 @@
20892089
<span class="hljs-number">0.34986</span>
20902090
</code></pre></div>
20912091

2092-
<div><h3 id="timingbyfunction">Timing by Function</h3><div><h4 id="generatesapngimageofacallgraphwithhighlightedbottlenecks">Generates a PNG image of a call graph with highlighted bottlenecks:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pycallgraph</span>
2093-
<span class="hljs-keyword">from</span> pycallgraph <span class="hljs-keyword">import</span> output, PyCallGraph
2094-
<span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> datetime
2095-
time_str = datetime.now().strftime(<span class="hljs-string">'%Y%m%d%H%M%S'</span>)
2096-
filename = <span class="hljs-string">f'profile-<span class="hljs-subst">{time_str}</span>.png'</span>
2097-
drawer = output.GraphvizOutput(output_file=filename)
2098-
<span class="hljs-keyword">with</span> PyCallGraph(drawer):
2099-
&lt;code_to_be_profiled&gt;
2100-
</code></pre></div></div>
2101-
2102-
2103-
<div><h3 id="profilingbyline">Profiling by Line</h3><pre><code class="text language-text">$ pip3 install line_profiler memory_profiler
2092+
<div><h3 id="profilingbyline">Profiling by Line</h3><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install line_profiler memory_profiler</span>
2093+
<span class="hljs-meta">@profile</span>
2094+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
2095+
a = [*range(<span class="hljs-number">10000</span>)]
2096+
b = {*range(<span class="hljs-number">10000</span>)}
2097+
main()
21042098
</code></pre></div>
21052099

21062100
<pre><code class="text language-text">$ kernprof -lv test.py
@@ -2119,6 +2113,17 @@
21192113
3 35.734 MiB 0.348 MiB a = [*range(10000)]
21202114
4 36.160 MiB 0.426 MiB b = {*range(10000)}
21212115
</code></pre>
2116+
<div><h3 id="callgraph">Call Graph</h3><div><h4 id="generatesapngimageofacallgraphwithhighlightedbottlenecks">Generates a PNG image of a call graph with highlighted bottlenecks:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pycallgraph</span>
2117+
<span class="hljs-keyword">from</span> pycallgraph <span class="hljs-keyword">import</span> output, PyCallGraph
2118+
<span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> datetime
2119+
time_str = datetime.now().strftime(<span class="hljs-string">'%Y%m%d%H%M%S'</span>)
2120+
filename = <span class="hljs-string">f'profile-<span class="hljs-subst">{time_str}</span>.png'</span>
2121+
drawer = output.GraphvizOutput(output_file=filename)
2122+
<span class="hljs-keyword">with</span> PyCallGraph(drawer):
2123+
&lt;code_to_be_profiled&gt;
2124+
</code></pre></div></div>
2125+
2126+
21222127
<div><h2 id="numpy"><a href="#numpy" name="numpy">#</a>NumPy</h2><p><strong>Array manipulation mini language. Can run up to one hundred times faster than equivalent Python code.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install numpy</span>
21232128
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
21242129
</code></pre></div>

0 commit comments

Comments
 (0)