netket.utils.timing.timed_scope

Contents

netket.utils.timing.timed_scope#

netket.utils.timing.timed_scope(name=None, force=False)[source]#

Context manager used to mark a scope to be timed individually by NetKet timers.

If name is not specified, the file name and line number is used.

If force is not specified, the timer only runs if a top-level timer is in use as well. If force is specified, the timer and nested timers will always run.

Example

Time a section of code

>>> import netket as nk
>>> import time
>>>
>>> with nk.utils.timing.timed_scope(force=True) as t:
...    time.sleep(1)  # This line and the ones below are indented
...    with nk.utils.timing.timed_scope("subfunction 1"):
...       time.sleep(0.5)
...    with nk.utils.timing.timed_scope("subfunction 2"):
...       time.sleep(0.25)
>>>
>>> t  
╭──────────────────────── Timing Information ─────────────────────────╮
│ Total: 1.763                                                        │
│ ├── (28.7%) | subfunction 1 : 0.505 s                               │
│ └── (14.3%) | subfunction 2 : 0.252 s                               │
╰─────────────────────────────────────────────────────────────────────╯
Parameters:
  • name (str) – Name to use for the timing of this line.

  • force (bool) – whether to always time, even if no top level timer is in use