[SOLVED] Choosing a style when referring to modules in Python
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is either style preferable? I'm guessing the second example would be preferable because it is less ambiguous which module is in use. What kind of difference is there in regards to cluttering either source code or memory? Maybe it makes more of a difference in longer scripts as opposed to shorter ones.
Preferable is subjective - I can't tell you which one you prefer, (nor which one I would prefer without a more concrete example).
Generally...
If you've got multiple different "Value"s then disambiguating the references obviously make sense.
If "Value" is unique within your script/project then doing so would be needless verbosity that distracts from what the code is doing.
The relevant Python docs have a third option of aliasing (import multiprocessing as mp) which balances reduced verbosity with being clear on what is being identified.
#!/usr/bin/python
#Module loading test
def t1():
import multiprocessing
def t2():
from multiprocessing import Process, Value, Array
def t3():
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt, QUrl, pyqtSignal
from PyQt5.QtNetwork import QNetworkCookie
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QApplication
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor
from PyQt5.QtWebEngineWidgets import (QWebEnginePage,
QWebEngineView, QWebEngineSettings)
def t4():
import PyQt5
Clear RAM before and between loading modules.
Code:
sudo sysctl vm.drop_caches=3
python -mtimeit -s 'import Test1' 'Test1.t1()'
1 loop, best of 5: 523 nsec per loop
:0: UserWarning: The test results are likely unreliable. The worst time (2.34 usec) was more than four times slower than the best time (523 nsec).
python -mtimeit -s 'import Test1' 'Test1.t1()'
1000000 loops, best of 5: 293 nsec per loop
python -mtimeit -s 'import Test1' 'Test1.t1()'
1000000 loops, best of 5: 288 nsec per loop
python -mtimeit -s 'import Test1' 'Test1.t1()'
1000000 loops, best of 5: 297 nsec per loop
sudo sysctl vm.drop_caches=3
python -mtimeit -s 'import Test1' 'Test1.t2()'
1 loop, best of 5: 1.94 usec per loop
:0: UserWarning: The test results are likely unreliable. The worst time (7.84 usec) was more than four times slower than the best time (1.94 usec)
python -mtimeit -s 'import Test1' 'Test1.t2()'
200000 loops, best of 5: 1.47 usec per loop
python -mtimeit -s 'import Test1' 'Test1.t2()'
200000 loops, best of 5: 1.47 usec per loop
python -mtimeit -s 'import Test1' 'Test1.t2()'
200000 loops, best of 5: 1.45 usec per loop
sudo sysctl vm.drop_caches=3
python -mtimeit -s 'import Test1' 'Test1.t3()'
1 loop, best of 5: 6.14 usec per loop
:0: UserWarning: The test results are likely unreliable. The worst time (26.3 usec) was more than four times slower than the best time (6.14 usec).
python -mtimeit -s 'import Test1' 'Test1.t3()'
50000 loops, best of 5: 5.82 usec per loop
python -mtimeit -s 'import Test1' 'Test1.t3()'
50000 loops, best of 5: 5.62 usec per loop
python -mtimeit -s 'import Test1' 'Test1.t3()'
50000 loops, best of 5: 5.69 usec per loop
sudo sysctl vm.drop_caches=3
python -mtimeit -s 'import Test1' 'Test1.t4()'
1000000 loops, best of 5: 301 nsec per loop
python -mtimeit -s 'import Test1' 'Test1.t4()'
1000000 loops, best of 5: 292 nsec per loop
python -mtimeit -s 'import Test1' 'Test1.t4()'
1000000 loops, best of 5: 303 nsec per loop
And those result are backwards from what I thought that I would get.
I thought that loading just what you needed was faster.
To the OP, I thought that is was faster importing just what you need from a module instead of the whole module.
I'm going to have to check that more when I get time. Then it depends on the module size if it makes any first launch time difference.
Python takes a while to load into RAM first instance anyway.
In case that is confusing. You'll need to call it depending on how you loaded it.
Code:
>>> from time import sleep
>>> for i in range(0, 5):
... print(i)
... sleep(1)
...
0
1
2
3
4
>>> import time
>>> for i in range(0, 5):
... print(i)
... time.sleep(1)
...
0
1
2
3
4
>>> from time import *
>>> for i in range(0, 5):
... print(i)
... sleep(1)
...
0
1
2
3
4
>>> import time as t
>>> for i in range(0, 5):
... print(i)
... t.sleep(1)
...
0
1
2
3
4
You're using code that doesn't exist in the wild with a testing methodolgy that is not remotely similar to live behaviour (at least for a GUI toolkit application).
Quote:
Originally Posted by Turbocapitalist
I'm primarily wondering if there are any differences in performance for any of the three methods.
Readability is important - make your code easy to understand and easy to maintain for future developers.
The slow parts of your code is not importing - if an application is behaving slowly, use a profiler against an actual running application to determine where the bottlenecks are.
There are [at least] two approaches: write "sort" code, I don't like long lines. That means we need to use your first example.
The other one: I would like to see where is a function coming from, that makes the code maintainable (a bit better).
You don't need to agree, you prefer what you like.
I think there will be no difference in speed, because the compiled code will be identical (also the time required to compile will be similar)
I also prefer to keep lines to 78 characters or less. Shorter is better. Yet I also usually need to be constantly reminded where functions have come from, to keep the code as clear and understandable as possible.
So I think the third way will be the most practical for now:
Code:
. . .
import time as t
import multiprocessing as mp
. . .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.