Python and STL efficiency

Pebblestone hadeshuang at gmail.com
Fri Aug 25 17:52:51 EDT 2006


> 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 heard that python's list is implemented as adjustable array.

Here's my lisp implementation:

++++++++++++++++++
(defun test-list ()
  (let ((a nil)
		(b nil))
	(dotimes (i 1000000)
	  (progn
		(push "What do you know" a)
		(push "so long ..." a)
		(push "chicken crosses road" a)
		(push "fool" a)))
	(setf b (remove-duplicates a))
	(map 'list #'print b)))
+++++++++++++++++++++++++

And the benchmark result:

(time (test-list))

"fool"
"chicken crosses road"
"so long ..."
"What do you know"
Evaluation took:
  2.88 seconds of real time
  2.744172 seconds of user run time
  0.136009 seconds of system run time
  0 page faults and
  74,540,392 bytes consed.

++++++++++++++++++++++++++++++

BTW, I couldn't install psyco on my system (ubuntu), gcc just prompt to
me thousands of lines of errors and warnings.




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