Newbie questions

Alex Martelli aleax at aleax.it
Mon Mar 24 13:14:03 EST 2003


Ladvánszky Károly wrote:

> I'd like to get help with the following issues.
> 
> 1.
> Is there a faster way to allocate a list of objects than this one?
> 
> ls=cnt*[None]
> for j in range(cnt):
>     ls[j]=TestClass(j)

Yes, but you can easily check such questions for yourself, particularly
thanks to the little jewel named timeit.py that comes with Python 2.3
(but works quite well even with 2.2 -- you can download it at
[one single BIG url...]:

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/python/python/dist/src/Lib/timeit.py?rev=1.9&content-type=text/plain

and then:

[alex at lancelot alex]$ python timeit.py -s 'def TestClass(x): return x' \
> -s 'cnt = 1111' \
> 'ls = cnt * [None]' \
> 'for j in range(cnt): ls[j] = TestClass(j)'
1000 loops, best of 3: 900 usec per loop
[alex at lancelot alex]$ python timeit.py -s 'def TestClass(x): return x' -s 
'cnt = 1111' 'ls = [TestClass(j) for j in range(cnt)]'
1000 loops, best of 3: 1.22e+03 usec per loop
[alex at lancelot alex]$

So, the preallocated list saves about 1220-900 = 300+ microseconds, for
a list of 1111 items, vs the obvious approach (a list comprehension); BUT:

[alex at lancelot alex]$ python timeit.py -s 'def TestClass(x): return x' -s 
'cnt = 1111' 'ls = map(TestClass,range(cnt))'
1000 loops, best of 3: 746 usec per loop
[alex at lancelot alex]$

built-in function map can slice another 150 microseconds or so (when
you need no lambda nor other extra levels of call to use it).

You presumably mean TestClass to be a class, not a function, but
that doesn't change the timings' rankings:

[alex at lancelot alex]$ python timeit.py -s 'class TestClass:' -s '  def 
__init__(self, j): self.j=j'  -s 'cnt = 1111' 'ls = 
map(TestClass,range(cnt))'
100 loops, best of 3: 4.36e+03 usec per loop
[alex at lancelot alex]$ python timeit.py -s 'class TestClass:' -s '  def 
__init__(self, j): self.j=j'  -s 'cnt = 1111' 'ls = [TestClass(j) for j in 
range(cnt)]'
100 loops, best of 3: 5.11e+03 usec per loop
[alex at lancelot alex]$

Rarely do you HAVE to wonder about such differences -- simplicity,
clarity, and the like are paramount -- but when you do, timeit.py
is your friend!

> 2.
> I understand that it's relatively easy to interface C++ DLLs to Python
> code. What are the main limitations? Can Python code use C++ objects, can
> they be pickle saved?

You can interface C++ code to Python quite easily with Boost (if your
mastery of C++ is superb and you spend some time mastering Boost just
as well -- if you think your mastery of C++ is NOT superb, I think you
might be best advised to stay as far from C++ as you can, for production
uses, until you HAVE mastered it thoroughly).  But objects coded in C
or C++ cannot be pickled unless, in their Python interface strata, you
expose suitable __getstate__ &c special methods -- Python cannot guess
what state each such object might need to save and restore, but you
may be able to express this.


Alex





More information about the Python-list mailing list