Note that there are some explanatory texts on larger screens.

plurals
  1. POimproving speed of Python module import
    primarykey
    data
    text
    <p>The question of how to speed up importing of Python modules has been asked previously (<a href="https://stackoverflow.com/questions/2010255/speeding-up-the-python-import-loader">Speeding up the python &quot;import&quot; loader</a> and <a href="https://stackoverflow.com/questions/6025635/python-speed-up-imports">Python -- Speed Up Imports?</a>) but without specific examples and has not yielded accepted solutions. I will therefore take up the issue again here, but this time with a specific example. </p> <p>I have a Python script that loads a 3-D image stack from disk, smooths it, and displays it as a movie. I call this script from the system command prompt when I want to quickly view my data. I'm OK with the 700 ms it takes to smooth the data as this is comparable to MATLAB. However, it takes an additional 650 ms to import the modules. So from the user's perspective the Python code runs at half the speed.</p> <p>This is the series of modules I'm importing:</p> <pre><code>import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation import scipy.ndimage import scipy.signal import sys import os </code></pre> <p>Of course, not all modules are equally slow to import. The chief culprits are:</p> <pre><code>matplotlib.pyplot [300ms] numpy [110ms] scipy.signal [200ms] </code></pre> <p>I have experimented with using <code>from</code>, but this isn't any faster. Since Matplotlib is the main culprit and it's got a reputation for slow screen updates, I looked for alternatives. One is PyQtGraph, but that takes 550 ms to import. </p> <p>I am aware of one obvious solution, which is to call my function from an interactive Python session rather than the system command prompt. This is fine but it's too MATLAB-like, I'd prefer the elegance of having my function available from the system prompt. </p> <p>I'm new to Python and I'm not sure how to proceed at this point. Since I'm new, I'd appreciate links on how to implement proposed solutions. Ideally, I'm looking for a simple solution (aren't we all!) because the code needs to be portable between multiple Mac and Linux machines. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload