scalene#

scalene erstellt sehr schnell Profile für CPU und Arbeitsspeicher. Dabei ist der Overhead üblicherweise mit 10–20% sehr gering.

Installation#

Linux, MacOS und WSL:

$ pipenv install scalene

Verwendung#

  1. Ein Beispielprogramm zum Profilieren

[1]:
import numpy as np

def profile_me():
    for i in range(6):
        x = np.array(range(10**7))
        y = np.array(np.random.uniform(0, 100, size=(10**8)))
  1. Scalene laden

[2]:
%load_ext scalene
Scalene extension successfully loaded. Note: Scalene currently only
supports CPU+GPU profiling inside Jupyter notebooks. For full Scalene
profiling, use the command line version.
  1. Profil mit nur einer Codezeile

[4]:
%scrun profile_me()
import numpy as np

def profile_me():
    for i in range(6):
        x = np.array(range(10**7))
        y = np.array(np.random.uniform(0, 100, size=(10**8)))
                                             [4]: % of time =  99.60% out of  14.95s.
        ╷        ╷       ╷       ╷
  Line  Time    –––––– ––––––                                                                                                 Python  native system [4]                                                                                             
╺━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
      1 │                      import numpy as np                                                                             
      2 │    1%     58%   40%                                                                                                 
      3 │                      %scrun profile_me()                                                                            
      4 │                      def profile_me():                                                                              
      5 │                          for i in range(6):                                                                         
      6 │                              x = np.array(range(10**7))                                                             
      7 │                              y = np.array(np.random.uniform(0, 100, size=(10**8)))                                  
        ╵        ╵       ╵       ╵

Erzeugen Sie ein reduziertes Profil (nur Zeilen mit Zählungen ungleich Null)

[5]:
%scrun --reduced-profile profile_me()
import numpy as np

def profile_me():
    for i in range(6):
        x = np.array(range(10**7))
        y = np.array(np.random.uniform(0, 100, size=(10**8)))
                                             [5]: % of time =  99.67% out of  15.19s.
        ╷        ╷       ╷       ╷
  Line  Time    –––––– ––––––                                                                                                 Python  native system [5]                                                                                             
╺━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
    ... │                      │
      2 │    1%     58%   40%  import numpy as np                                                                             
    ... │                      │
        ╵        ╵       ╵       ╵

Eine vollständige Liste der Optionen erhalter ihr mit:

[6]:
%scrun --help
usage: scalene [-h] [--version] [--outfile OUTFILE] [--html] [--json]
               [--reduced-profile] [--profile-interval PROFILE_INTERVAL]
               [--cpu-only] [--profile-all] [--profile-only PROFILE_ONLY]
               [--use-virtual-time]
               [--cpu-percent-threshold CPU_PERCENT_THRESHOLD]
               [--cpu-sampling-rate CPU_SAMPLING_RATE]
               [--malloc-threshold MALLOC_THRESHOLD]
               [--program-path PROGRAM_PATH] [--on | --off]

Scalene: a high-precision CPU and memory profiler, version 1.3.16
https://github.com/plasma-umass/scalene

command-line:
  % scalene [options] yourprogram.py
or
  % python3 -m scalene [options] yourprogram.py

in Jupyter, line mode:
  %scrun [options] statement

in Jupyter, cell mode:
  %%scalene [options]
   your code here


optional arguments:
  -h, --help            show this help message and exit
  --version             prints the version number for this release of Scalene and exits
  --outfile OUTFILE     file to hold profiler output (default: stdout)
  --html                output as HTML (default: text)
  --json                output as JSON (default: text)
  --reduced-profile     generate a reduced profile, with non-zero lines only (default: False)
  --profile-interval PROFILE_INTERVAL
                        output profiles every so many seconds (default: inf)
  --cpu-only            only profile CPU+GPU time (default: profile CPU+GPU, memory, and 
copying)
  --profile-all         profile all executed code, not just the target program (default: only
the target program)
  --profile-only PROFILE_ONLY
                        profile only code in filenames that contain the given strings,
separated by commas (default: no restrictions)
  --use-virtual-time    measure only CPU time, not time spent in I/O or blocking (default:
False)
  --cpu-percent-threshold CPU_PERCENT_THRESHOLD
                        only report profiles with at least this percent of CPU time (default:
1%)
  --cpu-sampling-rate CPU_SAMPLING_RATE
                        CPU sampling rate (default: every 0.01s)
  --malloc-threshold MALLOC_THRESHOLD
                        only report profiles with at least this many allocations (default:
100)
  --program-path PROGRAM_PATH
                        The directory containing the code to profile (default: the path to 
the profiled program)
  --on                  start with profiling on (default)
  --off                 start with profiling off

When running Scalene in the background, you can suspend/resume profiling
for the process ID that Scalene reports. For example:

   % python3 -m scalene  yourprogram.py &
 Scalene now profiling process 12345
   to suspend profiling: python3 -m scalene.profile --off --pid 12345
   to resume profiling:  python3 -m scalene.profile --on  --pid 12345

Profil mit mehr als einer Codezeile in einer Zelle

[7]:
%%scalene --reduced-profile
x = 0
for i in range(1000):
    for j in range(1000):
        x += 1
                                             [7]: % of time = 100.00% out of   0.10s.
        ╷        ╷       ╷       ╷
  Line  Time    –––––– ––––––                                                                                                 Python  native system [7]                                                                                             
╺━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
    ... │                      │
      4 │   11%      1%            for j in range(1000):                                                                      
      5 │   15%     53%   19%          x += 1                                                                                 
        ╵        ╵       ╵       ╵