empymod icon indicating copy to clipboard operation
empymod copied to clipboard

Improve / monitor CLI load time

Open prisae opened this issue 3 years ago • 2 comments

CLI load time

#160 introduces the CLI for empymod. However, the load time of empymod is not negligible. It was over 1.5s when starting with the CLI, #163 reduced it to below 1s, and it will hopefully come further down.

The main culprits are

  • matplotlib: lazy-loaded in #163
  • numba: this should improve significantly with numba v0.56.0
  • scipy.{special;optimize;fftpack;integrate}: lazy-loaded in #163 [^1]
  • scipy.interpolate: not much to do about, used in many places (but see [^1])
  • numpy: not much to do about, used everywhere; hopefully load time will improve upstream
  • scooby: FIXED since v0.6.0 (https://github.com/banesullivan/scooby/pull/85)

[^1]: SciPy introduced lazy loading of their submodules in v1.9.0. Once the minimum requirement can be bumped to v1.9.0, all the scipy imports should be rewritten and all custom lazy loading of scipy routines in empymod be removed

Status

python -Ximporttime -c "import empymod" 2> empymod.log
tuna empymod.log
  • empymod v2.1.5.dev3+g6797505.d20220723
    already with the improvements to empymod (main) and to scooby (in PR); but still without the numba improvements. 2022-07-23-01

prisae avatar Jul 22 '22 11:07 prisae

Very hacky idea how to "test/monitor" load time:

import subprocess

out = subprocess.run(
    ["python", "-X", "importtime", "-c", "import empymod"], capture_output=True
)
itime = int(out.stderr.decode("utf-8").split("\n")[-2].split('|')[1].strip())
assert itime < 1_000_000  # ideally much smaller of course!

Maybe a bit better (although not sure if cross-platform)

import subprocess

out = subprocess.run(
    ["time", "-f", "%U", "python", "-c", "import empymod"], capture_output=True
)
assert float(out.stderr.decode("utf-8")[:-1]) < 1

Update: This was implemented in #163

prisae avatar Jul 22 '22 11:07 prisae

From SciPy v1.9.0, we can do the following (see merged SciPy PR15230).

import scipy as sp
sp.interpolate

the implicit import from scipy import interpolate is not required any longer, and these modules are lazy loaded.

This includes the following methods that are currently lazy loaded in empymod, by putting the imports into the functions:

  • scipy.special (kernel.py, transform.py, utils.py)
  • scipy.fftpack (transform.py)
  • scipy.integrate (transform.py)
  • scipy.optimize (scripts/fdesign.py)

At some point in the future, we should bump the minimum SciPy version to 1.9, and replace all of them by sp.{module}.{function} calls.

prisae avatar Jul 23 '22 09:07 prisae

Load time from empymod reduced from > 1.5s to ~0.66 s with #163; and to ~0.15 s in #202 where minimum version of SciPy is 1.9, which yields with

numpy : 1.24.4
scipy : 1.11.1
numba : 0.57.1

the following import time:

  • empymod down from 0.656 s to 0.152 s
  • scipy down from 0.086 s to 0.004 s
  • numpy down from 0.175 s to 0.044 s
  • numba down from 0.353 s to 0.95 s

Selection_001

I am therefore closing this PR.

prisae avatar Feb 23 '24 12:02 prisae