From 4726291989823eefebf31385c887ff2c4c2cc677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 12 Sep 2019 03:38:07 +0200 Subject: [PATCH] Profiling --- README.md | 35 ++++++++++++++++++++--------------- index.html | 29 +++++++++++++++++------------ 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 2f93914..54e6aea 100644 --- a/README.md +++ b/README.md @@ -2434,22 +2434,14 @@ duration = perf_counter() - start_time 0.34986 ``` -### Timing by Function -#### Generates a PNG image of a call graph with highlighted bottlenecks: -```python -# $ pip3 install pycallgraph -from pycallgraph import output, PyCallGraph -from datetime import datetime -time_str = datetime.now().strftime('%Y%m%d%H%M%S') -filename = f'profile-{time_str}.png' -drawer = output.GraphvizOutput(output_file=filename) -with PyCallGraph(drawer): - -``` - ### Profiling by Line -```text -$ pip3 install line_profiler memory_profiler +```python +# $ pip3 install line_profiler memory_profiler +@profile +def main(): + a = [*range(10000)] + b = {*range(10000)} +main() ``` ```text @@ -2472,6 +2464,19 @@ Line # Mem usage Increment Line Contents 4 36.160 MiB 0.426 MiB b = {*range(10000)} ``` +### Call Graph +#### Generates a PNG image of a call graph with highlighted bottlenecks: +```python +# $ pip3 install pycallgraph +from pycallgraph import output, PyCallGraph +from datetime import datetime +time_str = datetime.now().strftime('%Y%m%d%H%M%S') +filename = f'profile-{time_str}.png' +drawer = output.GraphvizOutput(output_file=filename) +with PyCallGraph(drawer): + +``` + NumPy ----- diff --git a/index.html b/index.html index c75705e..0d5958b 100644 --- a/index.html +++ b/index.html @@ -2089,18 +2089,12 @@ duration = perf_counter() - start_time 0.34986 -

Timing by Function

Generates a PNG image of a call graph with highlighted bottlenecks:

# $ pip3 install pycallgraph
-from pycallgraph import output, PyCallGraph
-from datetime import datetime
-time_str = datetime.now().strftime('%Y%m%d%H%M%S')
-filename = f'profile-{time_str}.png'
-drawer = output.GraphvizOutput(output_file=filename)
-with PyCallGraph(drawer):
-    <code_to_be_profiled>
-
- - -

Profiling by Line

$ pip3 install line_profiler memory_profiler
+

Profiling by Line

# $ pip3 install line_profiler memory_profiler
+@profile
+def main():
+    a = [*range(10000)]
+    b = {*range(10000)}
+main()
 
$ kernprof -lv test.py
@@ -2119,6 +2113,17 @@ Line #    Mem usage    Increment   Line Contents
      3   35.734 MiB    0.348 MiB       a = [*range(10000)]
      4   36.160 MiB    0.426 MiB       b = {*range(10000)}
 
+

Call Graph

Generates a PNG image of a call graph with highlighted bottlenecks:

# $ pip3 install pycallgraph
+from pycallgraph import output, PyCallGraph
+from datetime import datetime
+time_str = datetime.now().strftime('%Y%m%d%H%M%S')
+filename = f'profile-{time_str}.png'
+drawer = output.GraphvizOutput(output_file=filename)
+with PyCallGraph(drawer):
+    <code_to_be_profiled>
+
+ +

#NumPy

Array manipulation mini language. Can run up to one hundred times faster than equivalent Python code.

# $ pip3 install numpy
 import numpy as np