Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should python imports take this long?

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?

like image 640
anon01 Avatar asked Aug 24 '15 02:08

anon01


People also ask

Do imports slow down Python?

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.

Should imports always be at the top Python?

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)

Does importing libraries slow down code?

Importing the unnecessary libraries will result in slowing down your code performance.


1 Answers

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.

like image 164
Nolan Akash Avatar answered Oct 16 '22 09:10

Nolan Akash