[Zope] Underscore Caracter

Duncan Booth duncan@rcp.co.uk
Wed, 6 Sep 2000 13:27:16 +0000


Tino Wildenhain wrote:

> > Turn "asd_asd" into "asd asd" and output:
> > 
> > <dtml-var "_.string.join(_.string.split('asd_asd', '_'))">
> > 
> I've seen this on plenty lines of digicools code and wondered
> about if this is more efficient then using just string.replace()
> ?
> I would say string.replace() should be a bit faster.

I think the reasons may be historical. For a long time the Zope 
documentation listed the join and split members of the string 
module, but whoever wrote the documentation the first time around 
missed out replace. So anyone coming at Zope from the DTML 
direction is likely not to spot the replace method.

Calling _.string.replace requires two attribute lookups and one 
python method call. replace itself requires zero or one memory 
allocation for the result.
Calling join/split requires four attribute lookups, two python method 
calls and n+3 memory allocations where n replacements are made 
(1 allocation when n is 0).

So I agree, replace should be faster, although given everything else 
that is going on, unless you do a lot of them you probably won't 
notice.

-- 
Duncan Booth                                             duncan@dales.rmplc.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?
http://dales.rmplc.co.uk/Duncan