Victor Stinner wrote: > Hum, copy-paste failure, I wrote numbers in the wrong order, it's: > > (test: Python 3.2 => Python 3.3) > "A".join(["Bob"]*100)): 0.92 => 2.11 > ("C"+"AB"*300).rfind("CA"): 0.57 => 1.03 > ("A" + ("Z"*128*1024)).replace("A", "BB", 1): 0.25 => 0.50 > > I improved str.replace(): it's now 5 times faster instead of 2 times slower > for this specific benchmark :-) (or 10 times faster in Python 3.3 before/after > my patch) Talking about str.replace, I was surprised to see this behaviour in 3.2: >>> s = 'spam' >>> t = s.replace('a', 'a') >>> s is t False Given that strings are immutable, would it not be an obvious optimization for replace to return the source string unchanged if the old and new substrings are equal, and avoid making a potentially expensive copy? I note that if count is zero, the source string is returned unchanged: >>> t = s.replace('a', 'b', 0) >>> t is s True -- Steven
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4