In [1]:
# An example program to profile

import numpy as np

def test_me():
    for i in range(6):
        x = np.array(range(10**7))
        y = np.array(np.random.uniform(0, 100, size=(10**8)))
In [2]:
# Load Scalene
%load_ext scalene
The scalene extension is already loaded. To reload it, use:
  %reload_ext scalene
In [3]:
# Profile just one line of code
%scrun test_me()
                                             [1]: % of time = 100.00% out of   6.02s.                                             
        ╷        ╷       ╷       ╷                                                                                                 
   Line Time    –––––– ––––––                                                                                                 Python  native system [1]                                                                                             
╺━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
      1 │        │       │       │# An example program to profile                                                                  
      2 │        │       │       │                                                                                                 
      3 │        │       │       │import numpy as np                                                                               
      4 │        │       │       │                                                                                                 
      5 │        │       │       │def test_me():                                                                                   
      6 │        │       │       │    for i in range(6):                                                                           
      7     1%   35% │   7%  │        x = np.array(range(10**7))                                                               
      8     2%   55% │       │        y = np.array(np.random.uniform(0, 100, size=(10**8)))                                    
        │        │       │       │                                                                                                 
╶───────┼────────┼───────┼───────┼────────────────────────────────────────────────────────────────────────────────────────────────╴
        │        │       │       │function summary for <ipython-input-14-3a53cdc5d2d5>                                             
      5     3%   91% │   6%  │test_me                                                                                          
        ╵        ╵       ╵       ╵                                                                                                 
In [4]:
# A full list of options
%scrun --help
usage: scalene [-h] [--version] [--outfile OUTFILE] [--html]
               [--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]

Scalene: a high-precision CPU and memory profiler, version 1.3.2
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]
   code...
   code...

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)
  --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 time (default: profile CPU, 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 files matching 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)

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

   % python3 -m scalene [options] 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
In [5]:
# Generate a reduced profile (only lines with non-zero counts)
%scrun --reduced-profile test_me()
                                             [1]: % of time = 100.00% out of   6.67s.                                             
        ╷        ╷       ╷       ╷                                                                                                 
   Line Time    –––––– ––––––                                                                                                 Python  native system [1]                                                                                             
╺━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
    ... │        │       │       │                                                                                                 
      7 │        │   34% │   7%  │        x = np.array(range(10**7))                                                               
      8     2%   49% │   8%  │        y = np.array(np.random.uniform(0, 100, size=(10**8)))                                    
        │        │       │       │                                                                                                 
╶───────┼────────┼───────┼───────┼────────────────────────────────────────────────────────────────────────────────────────────────╴
        │        │       │       │function summary for <ipython-input-14-3a53cdc5d2d5>                                             
      5     2%   83% │  15%  │test_me                                                                                          
        ╵        ╵       ╵       ╵                                                                                                 
In [6]:
%%scalene --reduced-profile
# Profile more than one line of code in a cell
x = 0
for i in range(1000):
    for j in range(1000):
        x += 1
         /var/folders/m7/sln1lr497jqcchb2ddh32rw00000gq/T/scalene_profile_qxy9xnh3.py: % of time = 100.00% out of   0.09s.         
        ╷        ╷       ╷       ╷                                                                                                 
   Line Time    –––––– ––––––                                                                                                 Python  native system /var/folders/m7/sln1lr497jqcchb2ddh32rw00000gq/T/scalene_profile_qxy9xnh3.py                     
╺━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
    ... │        │       │       │                                                                                                 
      4 │   21%  │       │       │    for j in range(1000):                                                                        
      5    59%    6% │  13%  │        x += 1                                                                                   
        ╵        ╵       ╵       ╵                                                                                                 
In [ ]: