does Python support some kind of "casting"

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Mar 15 09:25:19 EST 2002


Tino Lange <tl_news at nexgo.de> wrote in
news:a6shn9$9l6$1 at crusher.de.colt.net: 

>> Yes, that will make a copy. But copying a list is a pretty efficient
>> operation, so no, it isn't really horribly inefficient. Have you
>> tried it and identified this as a bottleneck? How long are your lists
>> anyway? 
> 
> 3600 Elements each.
> 
> No I didn't check if it's a bottleneck. But I immediately didn't feel
> good when writing "return list(newlist)"
> (the list contains a list of strings itself - so it's much stuff to
> copy)
 
When Python copies your list, it doesn't copy the strings, just adds a new 
reference to each string. So roughly speaking your list copy involves 
copying 3600 pointers and incrementing 3600 reference counts. My PC (900MHz 
pentium) can create (and destroy) 10,000 copies of a list of 3600 strings 
in about 1 second. It takes about 5 seconds if I save all the intermediate 
values giving me a list of 10,000 lists.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list