Python and STL efficiency

Pebblestone hadeshuang at gmail.com
Fri Aug 25 17:55:21 EDT 2006


Oh, I forgot.

Your python's example (use direct index array index) of my
corresponding lisp code works slower than the version which use
'append'.

This let me think how python's list is implemented.


Anyway, python's list is surprisingly efficient.


bearophileHUGS at lycos.com wrote:
> Pebblestone:
> > (defun test4 ()
> >   (let ((a (make-array 4000000 :element-type 'string
> > 					   :adjustable nil))
> > 		(b nil))
> > 	(dotimes (i 1000000)
> > 	  (progn
> > 		(let ((j (1- (* 4 i))))
> > 		  (setf (aref a (incf j)) "What do you know")
> > 		  (setf (aref a (incf j)) "so long ...")
> > 		  (setf (aref a (incf j)) "chicken crosses road")
> > 		  (setf (aref a (incf j)) "fool"))))
> > 	(setf b (remove-duplicates a))
> > 	(map 'vector #'print b)))
>
>
> That test4 function can be compared to this one, with explicit
> preallocation (and xrange instead of range!):
>
> def f2():
>     n = 1000000
>     a = [None] * n * 4
>     for i in xrange(0, n*4, 4):
>         a[i] = 'What do you know'
>         a[i+1] = 'so long...'
>         a[i+2] = 'chicken crosses road'
>         a[i+3] = 'fool'
>     for s in set(a):
>         print s
>
> But note this a list (that is an array, a list is a different data
> structure) of python becomes filled with pointers. I don't know what
> your CL does exactly.
>
> I can also suggest you to use Psyco too here
> (http://psyco.sourceforge.net/):
>
> import psyco
> psyco.bind(f2)
>
> It makes that f2 more than twice faster here.
> 
> Bye,
> bearophile




More information about the Python-list mailing list