Python and STL efficiency

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Aug 25 17:21:13 EDT 2006


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