I’m developing a script written with Python 3.8.10 and that uses external libraries installed with pip (e.g. torch: torch · PyPI). I’m running that script in Windows 10. The problem is that importing those libraries is very slow the first time I run the script after restarting my PC.
So I created the following example script:
t0 = time.time()
t1 = time.time()
This script takes more than 15 seconds after restarting the PC, while subsequent executions take less than 2 seconds.
Is it possible to speed up the library imports even after restarting the PC?
Just a wild guess here, but it sounds like maybe the first time you run the script after a reboot, your anti-virus scans all the files involved and that’s why it’s so slow.
Does importing torch start services or background processes?
I use Norton on my PC. I’ve tried disabling it though and I don’t seem to notice any difference in performance.
The timing difference could be the result of caching. It’s possible that the first time you
import torch after a reboot, a lot of data are loaded from disk into a memory cache. During the second import, the data are already in memory and don’t need to be loaded from disk again. If you have a slow hard drive such as the type with a spinning platter, then that could at least partly explain the large difference in runtime.
I’m in the same situation as you, the first time I import some larger libraries such as pandas, sklearn, torch will be very slow, often tens of seconds. Have you solved it?