Execution of import (was Re: boolean xor)

Delaney, Timothy tdelaney at avaya.com
Mon Jan 8 00:58:42 EST 2001


Tch tch tch. *Wrong* answer. That doesn't really do it ...
The answer *should* have been "So look in the code ..." ;)

Unfortunately, the time resolution of many platforms isn't sufficient to
determine this in the normal way of timing ...

def a():
	start = time.clock()
	from operator import truth
	end = time.clock()
	i = 1
	print (end - start)

is likely to give 0 every time.

A better timing option would be

def a():
	from operator import truth
	i = 1

def b():
	i = 1

a()

r = range(10000)

startA = time.clock()
for i in r:
	a()
endA = time.clock()

startB = time.clock()
for i in r:
	b()
endB = time.clock()

timeA = endA - startA
timeB = endB - startB
delta = timeA - timeB

print 'Time A:        ' + `timeA`
print 'Time B:        ' + `timeB`
print 'Delta (10000): ' + `delta`
print 'Delta (1):     ' + `float(delta) / len(r)`

Which turns up

Time A:        0.240446624583
Time B:        0.0198969603152
Delta (10000): 0.220549664268
Delta (1):     2.20549664268e-005

on my machine ... meaning that the overhead of calling

	from operator import truth

every time bar the first is bloody small, but can become significant over
enough iterations (0.2 of a second after 10000 iterations) and is indeed
considerably greater than the overhead of a function call.

Tim Delaney
Avaya Australia
+61 2 9352 9079

> -----Original Message-----
> From: Tim Peters [mailto:tim.one at home.com]
> Sent: Monday, 8 January 2001 4:24 PM
> To: python-list at python.org
> Subject: RE: Execution of import (was Re: boolean xor)
> 
> 
> [Delaney, Timothy]
> > I think the real question here though is ... how much overhead is
> > there in calling
> >
> > 	from operator import truch
> >
> > inside the function each time through (other than the first).
> > There is some overhead in determining that the module is already
> > imported (even though it should only be a dictionary lookup).
> 
> So time it!  That's easier than asking, and, even better, 
> you'll believe the
> result if you measure it yourself <0.9 wink>.
> 
> time.clock()-ly y'rs  - tim
> 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list