'import copy' too slow?, was Re: [Python-Dev] implementation of copy standard lib

Peter Otten __peter__ at web.de
Tue Aug 16 16:07:23 EDT 2005


[Martijn Brouwer]
> Importing copy takes 5-10 times more time that
> import os, string and re together!

Are you sure you aren't seeing the effects of caching? My little ad hoc test
(which fails on the os module) doesn't confirm your numbers:

$ python2.4 -m timeit -n1 -r1 -s"import sys; assert 're' not in sys.modules"
"import re"
1 loops, best of 1: 1.69 msec per loop

$ python2.4 -m timeit -n1 -r1 -s"import sys; assert 'copy' not in
sys.modules" "import copy"
1 loops, best of 1: 542 usec per loop

That's roughly one third of the time it takes to import re which I think is
vastly more popular.

$ python2.4 -m timeit -n1 -r1 -s"import sys; assert 'os' not in sys.modules"
"import os"
Traceback (most recent call last):
  File "/usr/local/lib/python2.4/timeit.py", line 264, in main
    r = t.repeat(repeat, number)
  File "/usr/local/lib/python2.4/timeit.py", line 188, in repeat
    t = self.timeit(number)
  File "/usr/local/lib/python2.4/timeit.py", line 161, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 3, in inner
AssertionError

Therefore the following number is meaningless:

$ python2.4 -m timeit -n1 -r1 "import os"
1 loops, best of 1: 9.06 usec per loop


Peter




More information about the Python-list mailing list