Re: Do more imported objects affect performance
At 2008-12-01T11:30:44Z, Nick Craig-Wood [EMAIL PROTECTED] writes: Importing the module is actualy slower... If you import the name into your namespace then there is only one lookup to do. If you import the module there are two. Note that if you're importing the entire module but want to call a function that many times, you can do something like: import timeit Timer = timeit.Timer for _ in xrange(100): Timer -- Kirk Strauser The Day Companies -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
On Tue, Dec 02, 2008 at 10:53:47PM -0500, Steve Holden wrote: Pardon me for intruding, but timings here are entirely the wrong focus for a Python newcomer. Given that imports are super-optimized (i.e. the code in the module is only performed once) such a small difference in timing is inconsequential, I would suggest. As long as from module import * is only ever used with modules specifically designed to support it, the other forms can be used as required. Sure, there is a timing difference between import module ... module.something() and from module import something ... something() but that's hardly the point. Learning to write sound Python is *much* more important that learning to write fast Python, and often the two coincide anyway. It was true when Kernighan and Plauger wrote it forty years ago and it's true now: First, make it work. Then, *if it doesn't work fast enough*, make it work faster. You are 100% right of course Steve. I was just trying to answer the specific question which is faster question which probably isn't helpful for new Python programmers to focus on. PS I enjoyed your book :-) -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
On Tue, Dec 02, 2008 at 11:24:29AM +0600, Taskinoor Hasan wrote: On Mon, Dec 1, 2008 at 8:21 PM, Filip Gruszczy?ski [EMAIL PROTECTED]wrote: I see. Thanks for a really good explanation, I like to know, how to do things in the proper way :) I always prefer to use import module and then use module.function. The reason is simple. It makes the code more readable and maintainable. I prefer the from module import function. That means that if module doesn't supply function it raises an exception at compile time, not run time when you try to run module.function. It then becomes very easy to see which functions you use from any given module too. It is also very slightly faster but that isn't a major consideration. PEP 8 endorses this style somewhat http://www.python.org/dev/peps/pep-0008/ - see the Imports section. [...] it's okay to say this though: from subprocess import Popen, PIPE [...] When importing a class from a class-containing module, it's usually okay to spell this from myclass import MyClass from foo.bar.yourclass import YourClass If this spelling causes local name clashes, then spell them import myclass import foo.bar.yourclass and use myclass.MyClass and foo.bar.yourclass.YourClass Ultimately it is a matter of taste I think! -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
Nick Craig-Wood a écrit : On Tue, Dec 02, 2008 at 11:24:29AM +0600, Taskinoor Hasan wrote: On Mon, Dec 1, 2008 at 8:21 PM, Filip Gruszczy?ski [EMAIL PROTECTED]wrote: I see. Thanks for a really good explanation, I like to know, how to do things in the proper way :) I always prefer to use import module and then use module.function. The reason is simple. It makes the code more readable and maintainable. I prefer the from module import function. That means that if module doesn't supply function it raises an exception at compile time, not run time when you try to run module.function. Nope. import is an executable statement, and ImportError happens at runtime too. -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
On Tue, 02 Dec 2008 11:12:31 +, Nick Craig-Wood wrote: I prefer the from module import function. That means that if module doesn't supply function it raises an exception at compile time, not run time when you try to run module.function. Wanna bet? def spam(): ... from math import harmonic_series ... return harmonic_series() ... dis.dis(spam) 2 0 LOAD_CONST 1 (-1) 3 LOAD_CONST 2 (('harmonic_series',)) 6 IMPORT_NAME 0 (math) 9 IMPORT_FROM 1 (harmonic_series) 12 STORE_FAST 0 (harmonic_series) 15 POP_TOP 3 16 LOAD_FAST0 (harmonic_series) 19 CALL_FUNCTION0 22 RETURN_VALUE spam() Traceback (most recent call last): File stdin, line 1, in module File stdin, line 2, in spam ImportError: cannot import name harmonic_series The same thing happens if the from...import is at the top level of the module, except that compilation is immediately followed by execution. It then becomes very easy to see which functions you use from any given module too. If that's important to you. Personally, I find it more useful to know where a function is defined. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
Steven D'Aprano [EMAIL PROTECTED] wrote: On Tue, 02 Dec 2008 11:12:31 +, Nick Craig-Wood wrote: I prefer the from module import function. That means that if module doesn't supply function it raises an exception at compile time, not run time when you try to run module.function. Wanna bet? def spam(): ... from math import harmonic_series ... return harmonic_series() ... dis.dis(spam) 2 0 LOAD_CONST 1 (-1) 3 LOAD_CONST 2 (('harmonic_series',)) 6 IMPORT_NAME 0 (math) 9 IMPORT_FROM 1 (harmonic_series) 12 STORE_FAST 0 (harmonic_series) 15 POP_TOP 3 16 LOAD_FAST0 (harmonic_series) 19 CALL_FUNCTION0 22 RETURN_VALUE spam() Traceback (most recent call last): File stdin, line 1, in module File stdin, line 2, in spam ImportError: cannot import name harmonic_series The same thing happens if the from...import is at the top level of the module, except that compilation is immediately followed by execution. You are technically right I am sure. However the error happens when you import the module with the error in, not when you run stuff from it which is the major difference. $ echo -e from os import sausage\n import_test.py $ python Python 2.5.2 (r252:60911, Sep 29 2008, 21:15:13) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. # This does produce an error import import_test Traceback (most recent call last): File stdin, line 1, in module File import_test.py, line 1, in module from os import sausage ImportError: cannot import name sausage $ # This produces an error also $ python import_test.py Traceback (most recent call last): File import_test.py, line 1, in module from os import sausage ImportError: cannot import name sausage $ Unlike $ echo -e import os\ndef f(): os.sausage\n import_test.py $ python Python 2.5.2 (r252:60911, Sep 29 2008, 21:15:13) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. # This doesn't produce an error import import_test # Until you actually call it import_test.f() Traceback (most recent call last): File stdin, line 1, in module File import_test.py, line 2, in f def f(): os.sausage AttributeError: 'module' object has no attribute 'sausage' $ # No error here either $ python import_test.py $ It then becomes very easy to see which functions you use from any given module too. If that's important to you. Personally, I find it more useful to know where a function is defined. We can agree to differ there I'm sure ;-) -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
Filip Gruszczyński wrote: [something I moved to after Nick's reply, where it belongs] 2008/12/1 Nick Craig-Wood [EMAIL PROTECTED]: Rafe [EMAIL PROTECTED] wrote: On Dec 1, 7:26?am, Filip Gruszczy?ski [EMAIL PROTECTED] wrote: I have following question: if I use from module import * instead from module import Class am I affecting performance of my program? I believe, that all those names must be stored somewhere, when they are imported and then browsed when one of them is called. So am I putting a lot of garbage to this storage and make those searches longer? Why use it if you don't need it? Your post implies a choice and the '*' import can really make things muddy if it isn't actually necessary (rare). Why not just import the module and use what you need? It is way easier to read/debug and maintains the name-space. Importing the module is actualy slower... If you import the name into your namespace then there is only one lookup to do. If you import the module there are two. $ python -m timeit -s 'from timeit import Timer' 'Timer' 1000 loops, best of 3: 0.0784 usec per loop $ python -m timeit -s 'import timeit' 'timeit.Timer' 100 loops, best of 3: 0.243 usec per loop I'm not suggestion you should ever use from module import * only ever import the things you actually need, eg from module import MyClass, my_function And here is the test again, actually calling something with the same difference in execution speed :- $ python -m timeit -s 'from os import nice' 'nice(0)' 100 loops, best of 3: 1.21 usec per loop $ python -m timeit -s 'import os' 'os.nice(0)' 100 loops, best of 3: 1.48 usec per loop I see. Thanks for a really good explanation, I like to know, how to do things in the proper way :) Pardon me for intruding, but timings here are entirely the wrong focus for a Python newcomer. Given that imports are super-optimized (i.e. the code in the module is only performed once) such a small difference in timing is inconsequential, I would suggest. As long as from module import * is only ever used with modules specifically designed to support it, the other forms can be used as required. Sure, there is a timing difference between import module ... module.something() and from module import something ... something() but that's hardly the point. Learning to write sound Python is *much* more important that learning to write fast Python, and often the two coincide anyway. It was true when Kernighan and Plauger wrote it forty years ago and it's true now: First, make it work. Then, *if it doesn't work fast enough*, make it work faster. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
On Dec 1, 7:26 am, Filip Gruszczyński [EMAIL PROTECTED] wrote: I have following question: if I use from module import * instead from module import Class am I affecting performance of my program? I believe, that all those names must be stored somewhere, when they are imported and then browsed when one of them is called. So am I putting a lot of garbage to this storage and make those searches longer? -- Filip Gruszczyński Why use it if you don't need it? Your post implies a choice and the '*' import can really make things muddy if it isn't actually necessary (rare). Why not just import the module and use what you need? It is way easier to read/debug and maintains the name-space. - Rafe -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
Rafe [EMAIL PROTECTED] wrote: On Dec 1, 7:26?am, Filip Gruszczy?ski [EMAIL PROTECTED] wrote: I have following question: if I use from module import * instead from module import Class am I affecting performance of my program? I believe, that all those names must be stored somewhere, when they are imported and then browsed when one of them is called. So am I putting a lot of garbage to this storage and make those searches longer? Why use it if you don't need it? Your post implies a choice and the '*' import can really make things muddy if it isn't actually necessary (rare). Why not just import the module and use what you need? It is way easier to read/debug and maintains the name-space. Importing the module is actualy slower... If you import the name into your namespace then there is only one lookup to do. If you import the module there are two. $ python -m timeit -s 'from timeit import Timer' 'Timer' 1000 loops, best of 3: 0.0784 usec per loop $ python -m timeit -s 'import timeit' 'timeit.Timer' 100 loops, best of 3: 0.243 usec per loop I'm not suggestion you should ever use from module import * only ever import the things you actually need, eg from module import MyClass, my_function And here is the test again, actually calling something with the same difference in execution speed :- $ python -m timeit -s 'from os import nice' 'nice(0)' 100 loops, best of 3: 1.21 usec per loop $ python -m timeit -s 'import os' 'os.nice(0)' 100 loops, best of 3: 1.48 usec per loop -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
I see. Thanks for a really good explanation, I like to know, how to do things in the proper way :) 2008/12/1 Nick Craig-Wood [EMAIL PROTECTED]: Rafe [EMAIL PROTECTED] wrote: On Dec 1, 7:26?am, Filip Gruszczy?ski [EMAIL PROTECTED] wrote: I have following question: if I use from module import * instead from module import Class am I affecting performance of my program? I believe, that all those names must be stored somewhere, when they are imported and then browsed when one of them is called. So am I putting a lot of garbage to this storage and make those searches longer? Why use it if you don't need it? Your post implies a choice and the '*' import can really make things muddy if it isn't actually necessary (rare). Why not just import the module and use what you need? It is way easier to read/debug and maintains the name-space. Importing the module is actualy slower... If you import the name into your namespace then there is only one lookup to do. If you import the module there are two. $ python -m timeit -s 'from timeit import Timer' 'Timer' 1000 loops, best of 3: 0.0784 usec per loop $ python -m timeit -s 'import timeit' 'timeit.Timer' 100 loops, best of 3: 0.243 usec per loop I'm not suggestion you should ever use from module import * only ever import the things you actually need, eg from module import MyClass, my_function And here is the test again, actually calling something with the same difference in execution speed :- $ python -m timeit -s 'from os import nice' 'nice(0)' 100 loops, best of 3: 1.21 usec per loop $ python -m timeit -s 'import os' 'os.nice(0)' 100 loops, best of 3: 1.48 usec per loop -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list -- Filip Gruszczyński -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
On Mon, Dec 1, 2008 at 8:21 PM, Filip Gruszczyński [EMAIL PROTECTED]wrote: I see. Thanks for a really good explanation, I like to know, how to do things in the proper way :) I always prefer to use import module and then use module.function. The reason is simple. It makes the code more readable and maintainable. 2008/12/1 Nick Craig-Wood [EMAIL PROTECTED]: Rafe [EMAIL PROTECTED] wrote: On Dec 1, 7:26?am, Filip Gruszczy?ski [EMAIL PROTECTED] wrote: I have following question: if I use from module import * instead from module import Class am I affecting performance of my program? I believe, that all those names must be stored somewhere, when they are imported and then browsed when one of them is called. So am I putting a lot of garbage to this storage and make those searches longer? Why use it if you don't need it? Your post implies a choice and the '*' import can really make things muddy if it isn't actually necessary (rare). Why not just import the module and use what you need? It is way easier to read/debug and maintains the name-space. Importing the module is actualy slower... If you import the name into your namespace then there is only one lookup to do. If you import the module there are two. $ python -m timeit -s 'from timeit import Timer' 'Timer' 1000 loops, best of 3: 0.0784 usec per loop $ python -m timeit -s 'import timeit' 'timeit.Timer' 100 loops, best of 3: 0.243 usec per loop I'm not suggestion you should ever use from module import * only ever import the things you actually need, eg from module import MyClass, my_function And here is the test again, actually calling something with the same difference in execution speed :- $ python -m timeit -s 'from os import nice' 'nice(0)' 100 loops, best of 3: 1.21 usec per loop $ python -m timeit -s 'import os' 'os.nice(0)' 100 loops, best of 3: 1.48 usec per loop -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list -- Filip Gruszczyński -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Do more imported objects affect performance
I have following question: if I use from module import * instead from module import Class am I affecting performance of my program? I believe, that all those names must be stored somewhere, when they are imported and then browsed when one of them is called. So am I putting a lot of garbage to this storage and make those searches longer? -- Filip Gruszczyński -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
Filip Gruszczyński wrote: I have following question: if I use from module import * instead from module import Class am I affecting performance of my program? I believe, that all those names must be stored somewhere, when they are imported and then browsed when one of them is called. So am I putting a lot of garbage to this storage and make those searches longer? Importing many (or all) names will take longer than importing a few, but you probably won't notice the difference. You'll also be filling the namespace with a lot of unwanted names. Will it affect how long it takes to look up a name? No. -- http://mail.python.org/mailman/listinfo/python-list