[Python-ideas] Create a StringBuilder class and use it everywhere

Antoine Pitrou solipsis at pitrou.net
Thu Aug 25 13:36:43 CEST 2011


On Thu, 25 Aug 2011 12:28:14 +0300
k_bx <k.bx at ya.ru> wrote:
> 
> The results are::
> 
>     kost at kost-laptop:~/tmp$ time python string_bucket_pure.py 
> 
>     real	0m7.194s
>     user	0m3.590s
>     sys	0m3.580s
>     kost at kost-laptop:~/tmp$ time python string_bucket_append.py 
> 
>     real	0m0.417s
>     user	0m0.330s
>     sys	0m0.080s
> 
> Fantastic, isn't it?
> 
> Also, now let's forget about speed and think about semantics a little: your task is: "build a string from it's pieces", or in other words "build a string from list of pieces", so from this point of view you can say that using [] and u"".join is better in semantic way.
> 
> Java has it's StringBuilder class for a long time

And Python has io.StringIO. I don't think we need to reinvent the wheel
under another name.
http://docs.python.org/library/io.html#io.StringIO

By the way, when prototyping snippets for the purpose of demonstrating
new features, you should really use Python 3, because Python 2 is in
bugfix-only mode.
(same applies to benchmark results, actually)

Regards

Antoine.





More information about the Python-ideas mailing list