For the following command
%time python test.py
on this script, test.py
import numpy as np
from math import *
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import LogNorm
from scipy import stats
I get the output:
real 0m1.933s
user 0m1.322s
sys 0m0.282s
Is there something wrong? Or is this how long imports should take?
Starting a Python interpreter and importing Python modules is relatively slow if you care about milliseconds. If you need to start hundreds or thousands of Python processes as part of a workload, this overhead will amount to several seconds of overhead.
Imports should always be written at the top of the file, after any module comments and docstrings. Imports should be divided according to what is being imported. There are generally three groups: standard library imports (Python's built-in modules)
Importing the unnecessary libraries will result in slowing down your code performance.
Some modules initialize when you use them, while others initialize everything once you start it up. Matplotlib is one of those modules.
Since matplotlib is a huge package that includes a whole lot of functionality, I'm not surprised that it takes this long, although it can get annoying.
So, in answer to your question, yes to some.
If you want a "solution" to your problem, you might want to import matplotlib only when you're going to use it, or have a loading screen / print
at the beginning of your program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With