Another question

Thomas Wouters thomas at xs4all.net
Mon Apr 17 16:12:13 EDT 2000


On Mon, Apr 17, 2000 at 09:22:05AM -0600, Jeff Massung wrote:
> Okay, another quick question. If it is quicker to do:

> import random
> from random import randint
> for i in range(1,1000000):
>     print randint(1,10)

> than this:

> import random
> for i in range(1,1000000):
>     print random.randint(1,10)

> Why would I not do "from <module> import *" with every module I load?

Everyone else already gave you good answers, but since you're primarily
concerned about minor speedups: Using 'from module import *' is a bad idea
if you want speed; it turns off certain optmizations ! Exactly the oposite
of what you intended :) See a posting by Tim Peters a couple of months back,
on optimizations.

The gist of the message was something like this: In absense of unpredicably
namespace modifying code ('from module import *' and 'exec something' in the
local namespace, if I recall correctly) all local variable lookups are
translated into a simple vector into the local namespace. So, 'dont do
that'. If you *insist* on that kind of speedup, import all items by name.

(But dont forget: if your module namespace gets too large, you'll end up
paying a price in performance ;)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list