From 4b2effdf7726ee9a2c8a7cd9ce45d1e119e7f06c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 10 Sep 2019 19:02:12 +0200 Subject: [PATCH] Profile --- README.md | 53 ++++++++++++++++++++++++++++------------------------- index.html | 44 +++++++++++++++++++++++--------------------- parse.js | 2 +- 3 files changed, 52 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 66a5b71..7cf72c9 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Contents **   ** **4. System:** **        ** **[`Print`](#print)**__,__ **[`Input`](#input)**__,__ **[`Command_Line_Arguments`](#command-line-arguments)**__,__ **[`Open`](#open)**__,__ **[`Path`](#path)**__,__ **[`Command_Execution`](#command-execution)**__.__ **   ** **5. Data:** **             ** **[`CSV`](#csv)**__,__ **[`JSON`](#json)**__,__ **[`Pickle`](#pickle)**__,__ **[`SQLite`](#sqlite)**__,__ **[`Bytes`](#bytes)**__,__ **[`Struct`](#struct)**__,__ **[`Array`](#array)**__,__ **[`MemoryView`](#memory-view)**__,__ **[`Deque`](#deque)**__.__ **   ** **6. Advanced:** **   ** **[`Threading`](#threading)**__,__ **[`Operator`](#operator)**__,__ **[`Introspection`](#introspection)**__,__ **[`Metaprograming`](#metaprograming)**__,__ **[`Eval`](#eval)**__,__ **[`Coroutine`](#coroutine)**__.__ -**   ** **7. Libraries:** **      ** **[`Progress_Bar`](#progress-bar)**__,__ **[`Plot`](#plot)**__,__ **[`Table`](#table)**__,__ **[`Curses`](#curses)**__,__ **[`Logging`](#logging)**__,__ **[`Scraping`](#scraping)**__,__ **[`Web`](#web)**__,__ **[`Profile`](#profile)**__,__ +**   ** **7. Libraries:** **      ** **[`Progress_Bar`](#progress-bar)**__,__ **[`Plot`](#plot)**__,__ **[`Table`](#table)**__,__ **[`Curses`](#curses)**__,__ **[`Logging`](#logging)**__,__ **[`Scraping`](#scraping)**__,__ **[`Web`](#web)**__,__ **[`Profile`](#profiling)**__,__ **                                 ** **[`NumPy`](#numpy)**__,__ **[`Image`](#image)**__,__ **[`Animation`](#animation)**__,__ **[`Audio`](#audio)**__,__ **[`Synthesizer`](#synthesizer)**__.__ @@ -2408,9 +2408,9 @@ def odds_handler(sport): ``` -Profile -------- -### Basic +Profiling +--------- +### Stopwatch ```python from time import time start_time = time() # Seconds since the Epoch. @@ -2418,7 +2418,7 @@ start_time = time() # Seconds since the Epoch. duration = time() - start_time ``` -### High Performance +#### High performance: ```python from time import perf_counter start_time = perf_counter() # Seconds since restart. @@ -2434,17 +2434,24 @@ duration = perf_counter() - start_time 0.34986 ``` -### Line Profiler +### Timing by Function +#### Generates a PNG image of a call graph with highlighted bottlenecks: ```python -# $ pip3 install line_profiler -@profile -def main(): - a = [*range(10000)] - b = {*range(10000)} -main() +# $ 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 ``` -#### Usage: ```text $ kernprof -lv test.py Line # Hits Time Per Hit % Time Line Contents @@ -2455,18 +2462,14 @@ Line # Hits Time Per Hit % Time Line Contents 4 1 2994.0 2994.0 72.6 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): - +```text +$ python3 -m memory_profiler test.py +Line # Mem usage Increment Line Contents +================================================ + 1 35.387 MiB 35.387 MiB @profile + 2 def main(): + 3 35.734 MiB 0.348 MiB a = [*range(10000)] + 4 36.160 MiB 0.426 MiB b = {*range(10000)} ``` diff --git a/index.html b/index.html index 53e15be..3209d86 100644 --- a/index.html +++ b/index.html @@ -216,7 +216,7 @@ pre.prettyprint { '4. System': [Print, Input, Command_Line_Arguments, Open, Path, Command_Execution], '5. Data': [CSV, JSON, Pickle, SQLite, Bytes, Struct, Array, MemoryView, Deque], '6. Advanced': [Threading, Operator, Introspection, Metaprograming, Eval, Coroutine], - '7. Libraries': [Progress_Bar, Plot, Table, Curses, Logging, Scraping, Web, Profile, + '7. Libraries': [Progress_Bar, Plot, Table, Curses, Logging, Scraping, Web, Profile, NumPy, Image, Animation, Audio, Synthesizer] } @@ -2070,14 +2070,14 @@ run(host='0.0.0.0', port='arsenal f.c.', 2.44, 3.29] -

#Profile

Basic

from time import time
+

#Profiling

Stopwatch

from time import time
 start_time = time()                     # Seconds since the Epoch.
 ...
 duration = time() - start_time
 
-

High Performance

from time import perf_counter
+

High performance:

from time import perf_counter
 start_time = perf_counter()             # Seconds since restart.
 ...
 duration = perf_counter() - start_time
@@ -2089,24 +2089,7 @@ duration = perf_counter() - start_time
 0.34986
 
-

Line Profiler

# $ pip3 install line_profiler
-@profile
-def main():
-    a = [*range(10000)]
-    b = {*range(10000)}
-main()
-
- -

Usage:

$ kernprof -lv test.py
-Line #      Hits         Time  Per Hit   % Time  Line Contents
-==============================================================
-     1                                           @profile
-     2                                           def main():
-     3         1       1128.0   1128.0     27.4      a = [*range(10000)]
-     4         1       2994.0   2994.0     72.6      b = {*range(10000)}
-
- -

Call Graph

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

# $ pip3 install pycallgraph
+

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')
@@ -2117,6 +2100,25 @@ drawer = output.GraphvizOutput(output_file=filename)
 
+

Profiling by Line

$ pip3 install line_profiler memory_profiler
+
+ +
$ kernprof -lv test.py
+Line #      Hits         Time  Per Hit   % Time  Line Contents
+==============================================================
+     1                                           @profile
+     2                                           def main():
+     3         1       1128.0   1128.0     27.4      a = [*range(10000)]
+     4         1       2994.0   2994.0     72.6      b = {*range(10000)}
+
+
$ python3 -m memory_profiler test.py
+Line #    Mem usage    Increment   Line Contents
+================================================
+     1   35.387 MiB   35.387 MiB   @profile
+     2                             def main():
+     3   35.734 MiB    0.348 MiB       a = [*range(10000)]
+     4   36.160 MiB    0.426 MiB       b = {*range(10000)}
+

#NumPy

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

# $ pip3 install numpy
 import numpy as np
 
diff --git a/parse.js b/parse.js index 7376dae..7cdb5ba 100755 --- a/parse.js +++ b/parse.js @@ -25,7 +25,7 @@ const TOC = ' \'4. System\': [Print, Input, Command_Line_Arguments, Open, Path, Command_Execution],\n' + ' \'5. Data\': [CSV, JSON, Pickle, SQLite, Bytes, Struct, Array, MemoryView, Deque],\n' + ' \'6. Advanced\': [Threading, Operator, Introspection, Metaprograming, Eval, Coroutine],\n' + - ' \'7. Libraries\': [Progress_Bar, Plot, Table, Curses, Logging, Scraping, Web, Profile,\n' + + ' \'7. Libraries\': [Progress_Bar, Plot, Table, Curses, Logging, Scraping, Web, Profile,\n' + ' NumPy, Image, Animation, Audio, Synthesizer]\n' + '}\n' + '
\n';